我想寫一個腳本,當玩家被敵人擊中時會重新加載關卡。我是gamedev和c#的新手,所以我對我的代碼不是100%自信。Unity2D Raycasting
我得到一些錯誤,把我扔掉,我找不到任何線上的東西指向正確的方向。
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'
道歉,如果這是可怕的格式,這是我在這個網站的第一篇文章:^) 感謝。
格式非常好!感謝您付出的努力! – Pogrindis