我正在尝试重新创建一个简单的激光拼图机制,就像在Talos原理中看到的那样 - 我有一个可以移动和旋转的激光发射器,当光束(光线投射和LineRenderer
)击中特定物体时,该对象将变得“活跃”。然而,当物体不再被激光击中时,它应该“停用”。
我在停用部分时遇到了麻烦。有没有办法告诉对象它不再被光线投射击中,或者可能会向LineRenderer
添加一个对撞机?或者第三种方式告诉对象它不再被任何激光击中。
答案 0 :(得分:1)
当您的目标被光线投射命中时,您可以使用RaycastHit参考来获取脚本并更新冷却时间。
让我们说我们有RaySender和RayReceiver。
RaySenderScript
public class RaySenderScript{
RaycastHit Hit;
void FixedUpdate(){
//SendRaycast and store in 'Hit'.
if (Hit.collider != null)
{ //If raycast hit a collider, attempt to acquire its receiver script.
RayReceiverScript = Hit.collider.gameObject.GetComponent<RayReceiverScript>();
if (RayReceiverScript != null)
{ //if receiver script acquired, hit it.
RayReceiverScript.HitWithRay();
}
}
}
}
RayReceiverScript
public class RayReceiverScript{
public float HitByRayRefreshTime = 1f;
float RayRunsOutTime;
public bool IsHitByRay = false;
void Start()
{ //Initialize run out time.
RayRunsOut = Time.time;
}
void Update()
{
if (Time.time > RayRunsOutTime)
{ //check if time run out, if it has, no longer being hit by ray.
IsHitByRay = false;
}
}
public void HitWithRay(){ //method activated by ray sender when hitting this target.
IsHitByRay = true;
RayRunsOutTime = Time.time + HitByRayRefreshTime;
}
}
答案 1 :(得分:0)
将激光命中的所有对象添加到集合中,并检查当前目标是否在此集合中。如果不存在,那么它就会被停用&#34;。