(UNITY)改变Raycast与LineRenderer Script碰撞的脚本中的值。

好吧,这是一个听起来很混乱的标题,但我很困,不知道如何用几个词来描述。基本上,我有一个激光的脚本,它可以折射出镜子,游戏的目的是将激光折射到接收器中,一旦第一个充满不同激光的房间与相应的接收器连接,门就会打开。我在Reciever脚本中使用了OnCollisionEnter,然后我发现lineRenderer不能被这个检测到。所以为了测试它,我使用了

if (gameObject.tag == "BLazer")
            {
                if (hit.collider.tag=="BTarget")
                {
                    GameObject BR11 = GameObject.Find("BlueReceiver11");
                    Receiver Receiver = BR11.GetComponent<Receiver>();
                    string TEST= hit.collider.name;
                    Receiver.HIT = true;

                    break;
                }

但这意味着我必须输入一个检查,并在脚本中为游戏中的每一个接收者输入对象,一定有比这更好的方法。

我在这里附上完整的脚本代码,忽略随机的bool和ints,全部用于测试。

鐳射商標

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

   [RequireComponent(typeof(LineRenderer))]
   public class Lazer : MonoBehaviour
   {
      public int reflections;
      public float maxLength;

    public static Lazer instance;

    private LineRenderer lineRenderer;
    private Ray ray;
    private RaycastHit hit;
    private Vector3 direction;
    public bool BL;
    public bool YL;
    public bool RL;
    public bool BL11;
    public bool BL12;
    public bool RL12;

    public int B;
    public int Y;
    public int R;

    private void Start()
    {
        instance = this;

    }

    private void Awake()
    {
        lineRenderer = GetComponent<LineRenderer>();
    }

    private void Update()
    {
        ray = new Ray(transform.position, transform.forward);

        lineRenderer.positionCount = 1;
        lineRenderer.SetPosition(0, transform.position);
        float remainingLength = maxLength;

        for (int i = 0; i < reflections; i++)
        {
            if (Physics.Raycast(ray.origin, ray.direction, out hit, remainingLength))
            {
                //Debug.Log("Transform Tag is: " + gameObject.tag);
                lineRenderer.positionCount += 1;
                lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
                remainingLength -= Vector3.Distance(ray.origin,hit.point);
                ray = new Ray(hit.point, Vector3.Reflect(ray.direction, hit.normal));

                //BLUE LASER
                if (gameObject.tag == "BLazer")
                {
                    if (hit.collider.tag=="BTarget")
                    {
                        GameObject BR11 = GameObject.Find("BlueReceiver11");
                        Receiver Receiver = BR11.GetComponent<Receiver>();
                        string TEST= hit.collider.name;
                        Receiver.HIT = true;

                        break;
                    }
                    else
                    {
                        //BL = false;
                        Debug.Log("Blue false");
                        if (hit.collider.tag != "Mirror")
                        {
                            break;
                        }
                    }
                }

                //YELLOW LASER
                else if (gameObject.tag == "YLazer")
                {
                    if (hit.collider.tag == "YTarget")
                    {
                        if (YL == false)
                        {
                            Y++;
                            YL = true;

                            Debug.Log("Yellow hit Yellow! - " + Y);
                        }
                        break;

                    }
                    else
                    {
                        if (YL)
                        {
                            Y--;
                            YL = false;
                            Debug.Log("Yellow false - "+Y );
                        }


                        if (hit.collider.tag != "Mirror")
                        {
                            break;
                        }
                    }
                }

                //RED LASER
                else if (gameObject.tag == "RLazer")
                {
                    if (hit.collider.tag == "RTarget")
                    {
                        Debug.Log("Red hit Red! - " + gameObject.tag + " hit " + hit.collider.tag);
                        RL = true;
                        break;
                    }
                    else {
                        RL = false;
                        Debug.Log("Red false");
                        if (hit.collider.tag != "Mirror")
                        {
                            break;
                        }
                    }  
                } 

            }
            else
            {

                lineRenderer.positionCount += 1;
                lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLength);
            }
        }
    }

 }

我的(现在很空)接收器类

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Receiver : MonoBehaviour
{
    public bool HIT;

    private void Update()
    {
        if (HIT) {
            Debug.Log("IT WORKED - " +gameObject.name);
        }
    }

第一会议室脚本

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Room1 : MonoBehaviour
{
    public GameObject Door1;
    public GameObject RedReceiver11;
    public GameObject RedReceiver12;
    public GameObject YellowReceiver11;
    public GameObject YellowReceiver12;
    public GameObject BlueReceiver11;
    public GameObject BlueReceiver22;

    public GameObject gdgfdg;


    public bool RR11, RR12, YR11, YR12, BR11, BR12;
    /*
    void Start()
    {
        GameObject thePlayer = GameObject.Find("First Person Player");
        PlayerMovement playerScript = thePlayer.GetComponent<PlayerMovement>();
        Debug.Log(playerScript.jumpHeight);
    }
    */
    private void TEST() {

        //RedReceiver11
    }


    void Update()
    {
        //If all lasers hitting recievers then raise the door
       if (RR11 && RR12 && YR11 && YR12 && BR11 && BR12)
        {
            Debug.Log("R1 DONE");
            if (Door1.transform.position.y < 8)
            {
                Door1.transform.Translate(Vector3.up * Time.deltaTime, Space.World);
            }

        }
       //if not, keep it close or lower it
        else {

            if (Door1.transform.position.y > 2)
            {
                Door1.transform.Translate(Vector3.down * Time.deltaTime, Space.World);
            }
        }

    }
}

解决方案:

如果因为某种原因,有人遇到了这个问题,而且他们用和我一样的方式来表达,我为你能找到这里的路而感到骄傲,你一定花了不少时间。

我忘了回到这个问题,但如果有人在未来寻找这个问题,并且被卡住了,我所做的是超级不聪明的,非常不可扩展性,但欢迎你去做,如果你正在寻找一个快速和肮脏的解决方案,lol

激光改变

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(LineRenderer))]
public class Lazer : MonoBehaviour
{
    public int reflections;
    public float maxLength;

    public static Lazer instance;

    private LineRenderer lineRenderer;
    private Ray ray;
    private RaycastHit hit;
    private Vector3 direction;


    private void Start()
    {
        instance = this;

    }

    private void Awake()
    {
        lineRenderer = GetComponent<LineRenderer>();
    }

    private void Update()
    {
        ray = new Ray(transform.position, transform.forward);

        lineRenderer.positionCount = 1;
        lineRenderer.SetPosition(0, transform.position);
        float remainingLength = maxLength;

        for (int i = 0; i < reflections; i++)
        {
            if (Physics.Raycast(ray.origin, ray.direction, out hit, remainingLength))
            {
                //Debug.Log("Transform Tag is: " + gameObject.tag);
                lineRenderer.positionCount += 1;
                lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
                remainingLength -= Vector3.Distance(ray.origin,hit.point);
                ray = new Ray(hit.point, Vector3.Reflect(ray.direction, hit.normal));

                //Level 1
                GameObject BR1 = GameObject.Find("BR1");
                Receiver Receiver1 = BR1.GetComponent<Receiver>();

                //Level 2
                GameObject YR2 = GameObject.Find("YR2");
                Receiver Receiver2 = YR2.GetComponent<Receiver>();

                //Level 3
                GameObject RR3 = GameObject.Find("RR3");
                Receiver Receiver3 = RR3.GetComponent<Receiver>();

                //Level 4
                GameObject BR4 = GameObject.Find("BR4");
                Receiver Receiver4A = BR4.GetComponent<Receiver>();
                GameObject YR4 = GameObject.Find("YR4");
                Receiver Receiver4B = YR4.GetComponent<Receiver>();

                //BLUE LASER LEVEL 3
                if (gameObject.name == "BL1")
                {
                    if (hit.collider.tag=="BTarget")
                    {
                        Receiver1.HIT = true;
                        break;
                    }
                    else
                    {
                        Receiver1.HIT = false;
                        if (hit.collider.tag != "Mirror")
                        {
                            break;
                        }
                    }
                }

                //YELLOW LASER LEVEL 2
                else if (gameObject.name == "YL2")
                {
                    if (hit.collider.tag == "YTarget")
                    {
                        Receiver2.HIT = true;
                        break;
                    }
                    else
                    {
                        Receiver2.HIT = false;
                        if (hit.collider.tag != "Mirror")
                        {
                            break;
                        }
                    }
                }

                //RED LASER LEVEL 3
                else if (gameObject.name == "RL3")
                {
                    if (hit.collider.tag == "RTarget")
                    {
                        Receiver3.HIT = true;
                        break;
                    }
                    else
                    {
                        Receiver3.HIT = false;
                        if (hit.collider.tag != "Mirror")
                        {
                            break;
                        }
                    }
                }


                //BLUE AND YELLOW LASER - LEVEL 4
                else if (gameObject.name == "BL4")
                {
                    if (hit.collider.tag == "BTarget")
                    {
                        Receiver4A.HIT = true;
                        break;
                    }
                    else
                    {
                        Receiver4A.HIT = false;
                        if (hit.collider.tag != "Mirror")
                        {
                            break;
                        }
                    }
                }

                else if (gameObject.name == "YL4")
                {
                    if (hit.collider.tag == "YTarget")
                    {
                        Receiver4B.HIT = true;
                        break;
                    }
                    else
                    {
                        Receiver4B.HIT = false;
                        if (hit.collider.tag != "Mirror")
                        {
                            break;
                        }
                    }
                }




                else if (gameObject.tag == "BLazer"|| gameObject.tag == "YLazer" || gameObject.tag == "RLazer")
                {
                    if (hit.collider.tag != "Mirror")
                    {
                        break;
                    }

                }

            }
            else
            {

                lineRenderer.positionCount += 1;
                lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLength);
            }
        }
    }

}

然后我用一个GameManager来管理关卡。

游戏经理

using System.Collections;
using System.Collections.Generic; using UnityEngine;

public class GM : MonoBehaviour { public GameObject Gate1; public GameObject DoorL1; public GameObject BR1; //Level 1 Receiver public bool R1;

public GameObject Gate2;
public GameObject DoorL2;
public GameObject YR2; //Level 2 Receiver
public bool R2;

public GameObject Gate3;
public GameObject DoorL3;
public GameObject RR3; //Level 3 Receiver
public bool R3;

public GameObject Gate4;
public GameObject DoorL4A;
public GameObject DoorL4B;
public GameObject BR4; //Level 4 Receiver Blue
public GameObject YR4; //Level 4 Receiver Yellow
public bool R4;
private bool L1Played;
private bool L2Played;
private bool L3Played;
private bool L4Played;

void Update()
{

    //ROOM 1 

    Receiver R1Script = BR1.GetComponent<Receiver>();
    //If all lasers hitting recievers then raise the door

    if (R1Script.HIT)
    {
        Debug.Log("R1 DONE");
        if (Gate1.transform.position.y < 8)
        {
            Gate1.transform.Translate(Vector3.up * Time.deltaTime, Space.World);
            if (L1Played == false)
            {
                Gate1.GetComponent<AudioSource>().Play();
                L1Played = true;
            }
            //FindObjectOfType<AudioManager>().Play("LevelComplete");
            DoorL1.SetActive(false);
        }

    }
   //if not, keep it close or lower it
    else {

        if (Gate1.transform.position.y > 2)
        {
            Gate1.transform.Translate(Vector3.down * Time.deltaTime, Space.World);
            DoorL1.SetActive(true);
        }
    }



    //ROOM 2

    Receiver R2Script = YR2.GetComponent<Receiver>();
    //If all lasers hitting recievers then raise the door

    if (R2Script.HIT)
    {
        Debug.Log("R2 DONE");
        if (Gate2.transform.position.y < 8)
        {
            Gate2.transform.Translate(Vector3.up * Time.deltaTime, Space.World);
            //FindObjectOfType<AudioManager>().Play("LevelComplete");
            if (L2Played == false)
            {
                Gate2.GetComponent<AudioSource>().Play();
                L2Played = true;
            }
            DoorL2.SetActive(false);
        }

    }
    //if not, keep it close or lower it
    else
    {

        if (Gate2.transform.position.y > 2)
        {
            Gate2.transform.Translate(Vector3.down * Time.deltaTime, Space.World);
            DoorL2.SetActive(true);
        }
    }

    //ROOM 3

    Receiver R3Script = RR3.GetComponent<Receiver>();
    //If all lasers hitting recievers then raise the door

    if (R3Script.HIT)
    {
        Debug.Log("R3 DONE");
        if (Gate3.transform.position.y < 8)
        {
            Gate3.transform.Translate(Vector3.up * Time.deltaTime, Space.World);
            //FindObjectOfType<AudioManager>().Play("LevelComplete");
            if (L3Played == false)
            {
                Gate3.GetComponent<AudioSource>().Play();
                L3Played = true;
            }
            DoorL3.SetActive(false);
        }

    }
    //if not, keep it close or lower it
    else
    {

        if (Gate3.transform.position.y > 2)
        {
            Gate3.transform.Translate(Vector3.down * Time.deltaTime, Space.World);
            DoorL3.SetActive(true);
        }
    }


    //ROOM 4

    Receiver R4ScriptA = BR4.GetComponent<Receiver>();
    Receiver R4ScriptB = YR4.GetComponent<Receiver>();
    //If all lasers hitting recievers then raise the door

    if (R4ScriptA.HIT && R4ScriptB.HIT)
    {
        Debug.Log("R4 DONE");
        if (Gate4.transform.position.y < 8)
        {
            Gate4.transform.Translate(Vector3.up * Time.deltaTime, Space.World);
            //FindObjectOfType<AudioManager>().Play("LevelComplete");
            if (L4Played == false)
            {
                Gate4.GetComponent<AudioSource>().Play();
                L4Played = true;
            }
            DoorL4A.SetActive(false);
            DoorL4B.SetActive(false);
        }

    }
    //if not, keep it close or lower it
    else
    {

        if (Gate4.transform.position.y > 2)
        {
            Gate4.transform.Translate(Vector3.down * Time.deltaTime, Space.World);
            DoorL4A.SetActive(false);
            DoorL4B.SetActive(false);
        }
    }
}
}

给TA打赏
共{{data.count}}人
人已打赏
未分类

rails中 "root to: "和 "root "有什么区别?

2022-10-22 9:43:23

未分类

并行和顺序的事件处理

2022-10-22 9:43:25

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索