# How to create laser redirection?

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

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:

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


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.
• That's not what it looks like from this end. Considering I'm seeing two different classes outLaser and sourceLaser1. I'm suggesting making a single class that contains all the logic. For example, the reason it's only working with one redirect right now is because you can only check if a sourceLaser1 has a hit (reference.sourceHit.collider). That won't work for the next redirect cube because the laser hitting the next redirect cube is coming from a outLaser not a sourceLaser1. Make sense? – MichaelHouse Dec 4 '14 at 22:18
• I believe my existing answer is how this should be done. The problem with your modified code is that the first line checks reference.sourceHit, that conditional can only be true if reference has been set, and since reference is of the type sourceLaser1, a cube being struck by an outLaser is never going to get past that conditional. So only cubes being stuck by a sourceLaser1 can be activated, meaning your lasers won't go farther than the source and out the first redirect. Try setting it up the way I've suggested as it will meet your requirements and be considerably cleaner code. – MichaelHouse Dec 7 '14 at 0:28