Search code examples
c#unity-game-engineraycasting

(UNITY) Changing value in the script that Raycast is colliding with from the LineRenderer Script


Okay that was a very confusing sounding title but i'm very stuck and im not sure how to describe in few words. So basically I have a script for a laser that can refract off mirrors and the object of the game is to refract the lasers into the Receivers, once the first room full of different lasers has connected with their corresponding receiver, the door will open. I used a OnCollisionEnter in the Reciever script before i found out that lineRenderer cannot be detected by this. So then to test it i used

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

Which works, however this would mean i would have to type out a check and bring in the objects for every single Receiver in the Game in a script There has to be a better way than this

Ill include the code here for the full scripts, ignore the random bools and ints, all for testing

LASER SCRIPT

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

 }

My (pretty empty now) Receiver class

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

Room 1 Script

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

    }
}

Solution

  • If for some reason someone else has this problem, and they worded it in the same way i did, im proud of you for finding your way here, it must have taken a while.

    I forgot to come back to this but if anyone is looking for this in the future and is stuck, what i did is super ineligent and very non scalable but your welcome to go for it if you're looking for a quick and dirty solution lol

    Laser Changes

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

    And then i used a GameManager for the levels

    Game Manager

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