【发布时间】:2015-11-03 20:18:24
【问题描述】:
我正在尝试编写一个脚本,当玩家被敌人击中时会重新加载关卡。我是 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'
抱歉,如果这格式很糟糕,这是我在这个网站上的第一篇文章 :^) 谢谢。
【问题讨论】:
-
格式非常好!感谢您的努力!