2
\$\begingroup\$

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; 
    }
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Excuse me if I'm wrong, but isn't that what I did? The redirect cube has it's own laser component, when a raycast from the source cube hits the redirect, the redirect fires up its own laser. \$\endgroup\$ – darkvalley Dec 4 '14 at 22:08
  • \$\begingroup\$ 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? \$\endgroup\$ – MichaelHouse Dec 4 '14 at 22:18
  • \$\begingroup\$ I see, thanks. Is there any way I can get it to detect outLaser raycasts as well? \$\endgroup\$ – darkvalley Dec 6 '14 at 19:22
  • \$\begingroup\$ I've updated my original post with the new code I have, I think I almost got it. Not sure what to do now. \$\endgroup\$ – darkvalley Dec 6 '14 at 20:04
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Dec 7 '14 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.