How to create laser redirection?

| | August 5, 2015

Think portal 2, where a laser can go through a cube, and depending on the rotation of that cube, it will redirect it. Examples:

enter image description here

Here is my barely functioning code. It works for one cube, I’m able to get the original (source) laser to hit any ‘redirect’ cube, then I’m able to cast a line renderer and change the rotation.

Problem comes when I try to use the redirect laser to hit another redirect cube, I can’t figure out how to make this code work for multiple cubes hitting off each other.

using UnityEngine;
using System.Collections;

public class outLaser : MonoBehaviour
{
    public sourceLaser1 reference; //referring to another class that has the original (source) laser

    LineRenderer redirectLaser;
    RaycastHit redirectHit;

    void Awake() 
    {
        redirectLaser = GetComponent<LineRenderer>();

    }

    void Update()
    {
        if(reference.sourceHit.collider.gameObject == transform.parent.gameObject)
        {
            if(Physics.Raycast(transform.position, -transform.forward, out redirectHit, Mathf.Infinity))
            {
                redirectLaser.enabled = true;
                redirectLaser.SetPosition(0, transform.position);
                redirectLaser.SetPosition(1, redirectHit.point);
            }
        }
        else
        {
            redirectLaser.enabled = false;
        }
    }
}

Tried many different solutions, all with no avail.

Here’s what it looks like:

enter image description here

enter image description here

enter image description here

EDIT: I’ve gotten this far

    if(reference.sourceHit.collider.gameObject == transform.parent.gameObject)
    {
        if(Physics.Raycast(transform.position, -transform.forward, out redirectHit, Mathf.Infinity))
        {
            redirectLaser.enabled = true;
            redirectLaser.SetPosition(0, transform.position);
            redirectLaser.SetPosition(1, redirectHit.point);

            //Logic that allows redirect cube to accept redirect raycasts
            cubeThatsHit = redirectHit.collider.gameObject; 

            if(cubeThatsHit.gameObject.tag == "Redirect")       //if the cube that is hit is a redirect cube, do this
            {
                   //This is where I need the logic
            }
        }
    }
    else
    {
        redirectLaser.enabled = false; 
    }

One Response to “How to create laser redirection?”

  1. You should just have one component for your laser. The source object would just have the laser component. The “redirect” object will have an additional component called redirect. The laser component casts a ray, if that ray hits an object containing a redirect component, the “redirect” object powers up its own laser.

    Think of the laser more as a trigger to power up the cubes. When the cubes are powered up, they emit their own laser in the direction defined by their rotation. This allows you to redirect the laser as many times as needed.

Leave a Reply