gpt4 book ai didi

c# - Unity - 使用光线转换激光激活物体

转载 作者:太空宇宙 更新时间:2023-11-03 12:16:13 24 4
gpt4 key购买 nike

我正在尝试重新创建一个简单的激光拼图机制,就像在 Talos Principle 中看到的那样 - 我有一个可以移动和旋转的激光发射器,当光束(光线转换和 LineRenderer)命中时一个特定的对象,该对象将变得“活跃”。然而,当物体不再被激光击中时,它应该“停用”。

我在停用部分遇到了问题。有没有办法告诉对象它不再被光线转换击中,或者向 LineRenderer 添加一个碰撞器?或者第三种方式告诉物体它不再被任何激光击中。

最佳答案

当您的目标被光线转换击中时,您可以使用 RaycastHit 引用获取脚本并更新冷却时间。

假设我们有 RaySender 和 RayReceiver。

光线发送脚本

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();
}
}
}
}

光线接收脚本

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. 发送者用射线撞击接收者。
  2. Sender 有一个 Receiver 的引用,它使用 GetComponent() 来访问它。然后它可以说 receiverScript.HitWithRay();
  3. 接收器不断检查它是否不再接收,如果没有,它就停止被射线击中。

关于c# - Unity - 使用光线转换激光激活物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49646247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com