我在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");
}
1 回答
你可以尝试一下我使用略有不同的方法: