我正在尝试写一个脚本,当玩家被敌人击中时,它会重新加载关卡。我刚接触gamedev和c#,所以我对我的代码不是100%有信心。
我收到了一些错误,它们让我迷失了方向,而且我在网上找不到任何可以指引我正确方向的东西。
using UnityEngine;
using System.Collections;
public class RobotAttack : MonoBehaviour
{
void Update()
{
RaycastHit2D hit;
Vector2 attackPosition = transform.position + new Vector2(0f, 1f);
if (Physics2D.Raycast(attackPosition, transform.forward, hit, 1f) && (hit.transform.tag == "Player"))
{
Application.LoadLevel(Application.loadedLevel);
}
}
}我得到的错误如下所示。
Assets/Scripts/RobotAttack.cs(14,44): error CS0121: The call is ambiguous between the following methods or properties: `UnityEngine.Vector2.operator +(UnityEngine.Vector2, UnityEngine.Vector2)' and `UnityEngine.Vector3.operator +(UnityEngine.Vector3, UnityEngine.Vector3)'
Assets/Scripts/RobotAttack.cs(17,66): error CS0165: Use of unassigned local variable `hit'
Assets/Scripts/RobotAttack.cs(17,23): error CS1502: The best overloaded method match for `UnityEngine.Physics2D.Raycast(UnityEngine.Vector2, UnityEngine.Vector2, float, int)' has some invalid arguments
Assets/Scripts/RobotAttack.cs(17,23): error CS1503: Argument `#3' cannot convert `UnityEngine.RaycastHit2D' expression to type `float'如果这篇文章的格式很糟糕,很抱歉,这是我在这个网站上的第一篇帖子:^)谢谢。
发布于 2015-11-03 20:08:41
Assets/Scripts/Robotack.cs(14,44):错误CS0121:以下方法或属性之间的调用不明确:
UnityEngine.Vector2.operator +(UnityEngine.Vector2, UnityEngine.Vector2)' andUnityEngine.Vector3.operator +(UnityEngine.Vector3,UnityEngine.Vector3)‘
这意味着有另一个具有相同名称的方法,因此编译器不知道您打算使用哪个方法。也就是说:Vector2.operator和Vector3.operator更改名称将解决这个问题,或者指定前缀库。
Assets/Scripts/RobotAttack.cs(17,66):错误命中:使用未赋值的局部变量‘CS0165’
这非常明确,您没有将hit定义为任何类型,只定义了它的类型。你可以试试:RaycastHit2D hit = new RaycastHit2D();
但我不太确定它的建造者。
编辑:查看构造函数后的:
Raycast Documentation
Assets/Scripts/RobotAttack.cs(17,23):错误CS1502:
UnityEngine.Physics2D.Raycast(UnityEngine.Vector2, UnityEngine.Vector2, float, int)' has some invalid arguments Assets/Scripts/RobotAttack.cs(17,23): error CS1503: Argument#3的最佳重载方法匹配‘无法转换UnityEngine.RaycastHit2D' expression to type浮点’
这仅仅意味着您试图传递的参数的类型错误。值得注意的是,第三个参数不是float,实际上它看起来像是传入了一个RaycastHit2D类型的对象。你可以尝试将它转换为一个浮点数,如果你有信心它会转换的话,尽管这是非常不可能的。或者,您需要建立要使用的正确输入变量。
希望这能对你有所帮助!
https://stackoverflow.com/questions/33498639
复制相似问题