我在 Unity 中设置了光线投射,它根据对象的标签检测我何时查看对象。现在,我想做的是有一个定时事件,当我在一段时间内查看我的标记对象时触发,然后,当我移开视线时,我希望计时器重置。
但是,我在让它工作时遇到了一些真正的麻烦。有人可以看看我的代码并指出我做错了什么吗?
public float end_time = 10.0f;
public float start_time;
// Use this for initialization
void Start ()
{
start_time = Time.deltaTime;
}
// Update is called once per frame
void Update ()
{
EyeTarget();
}
void EyeTarget()
{
RaycastHit hit;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, out hit))
{
// Debug.Log("ray hit (tag): " + hit.collider.gameObject.tag + " : ");
if(hit.collider.gameObject.tag == "floor")
{
Debug.Log("Just hit the floor");
start_time = Time.time - end_time;
if(start_time <= 0)
{
Debug.Log("looked at floor for 10 seconds");
}
}
if( hit.collider.gameObject.tag != "floor")
{
ResetTimer();
}
}
Debug.DrawRay(transform.position, fwd, Color.green);
}
void ResetTimer()
{
start_time = Time.time;
Debug.Log("resetting timer");
}