using UnityEngine;
using System.Collections;
public class GuardSample : MonoBehaviour
{
FOV2DEyes eyes;
FOV2DVisionCone visionCone;
float speed = -5;
RaycastHit hit;
void Start()
{
eyes = GetComponentInChildren<FOV2DEyes>();
visionCone = GetComponentInChildren<FOV2DVisionCone>();
}
void FixedUpdate()
{
if (transform.position.x < -10 || transform.position.x > 10)
{
speed *= -1;
}
transform.position = new Vector3(transform.position.x + speed * Time.fixedDeltaTime, transform.position.y, transform.position.z);
}
bool playerInView = false;
void Update()
{
playerInView = false;
foreach (RaycastHit hit in eyes.hits)
{
if (hit.transform && hit.transform.tag == "Player")
{
playerInView = true;
}
}
}
void OnGUI()
{
if (playerInView)
{
GUI.Box (new Rect (10, 10, 160, 60), "Title");
GUI.Label( new Rect(10, 10, 160, 60), hit.collider.gameObject.name);
}
}
}
我的 gaurd 移动,当玩家进入 raycast 时,GUI 出现但名称无法识别
除“hit.collider.gameObject.name”外,一切正常 Unity 给出“对象引用未设置为对象实例”的错误
请看看我是 Unity 和 C# 的新手