Unity-使用raycast激光激活的对象 [英] Unity - Laser activated objects using raycast

查看:115
本文介绍了Unity-使用raycast激光激活的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新创建一个简单的激光拼图机制,如《塔洛斯原理》中所见-我有一个可以移动和旋转的激光发射器,并且当光束(射线投射和LineRenderer)撞击特定对象时,对象将变为活动".但是,当物体不再被激光撞击时,它应该停用".

I am trying to recreate a simple laser puzzle mechanic like seen in The Talos Principle - where i have a laser emitter that i can move and rotate, and when the beam (raycast and LineRenderer) hits a specific object, that object will become "active". However when the object is no longer being hit by a laser it should "deactivate".

我在停用部分方面遇到麻烦.有没有办法告诉对象它不再被射线投射击中,或者可以向LineRenderer添加对撞机?或者用第三种方式告诉物体它不再被激光击中.

I am having troubles with the Deactivate part. Is there a way to tell the object that it is no longer being hit by a raycast, or add a collider to the LineRenderer maybe? Or some third way to tell the object that it is no longer being hit by any lasers.

推荐答案

当目标被射线投射击中时,您可以使用RaycastHit参考获取脚本并更新冷却时间.

When your target is hit by a raycast, you could use the RaycastHit reference to acquire a script and update the cooldown.

让我们说我们有RaySender和RayReceiver.

Lets say we have RaySender and RayReceiver.

RaySenderScript

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

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. 发件人用射线击中接收器.
  2. 发件人具有对Receiver的引用,它使用GetComponent()对其进行访问.然后它可以说receiveScript.HitWithRay();
  3. 接收器不断检查它是否不再接收,如果不是,则不再被射线击​​中.

这篇关于Unity-使用raycast激光激活的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆