Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Laser reflection help requested!

A topic by Fobok created Jun 15, 2017 Views: 1,648 Replies: 3
Viewing posts 1 to 3
Submitted

I'm working on a game that's quite similar to probably what a lot of people are doing, but as this is my first attempt to make a game in Unity that wasn't just a textbased adventure, I thought I'd just go for it. The game will involve bouncing a mounted laser off of mirrors to hit a target at the end of a first person maze. 

For the last few days, I've been struggling with a problem with my laser. Until this morning, I thought the problem had to do with getting it to stop when it hit a target that wasn't a mirror. It would, seemingly, veer off randomly when I added the if statement to check for collision. However, in setting up to make the video below, I discovered the problem goes deeper. It's happening whenever I increase the number of reflections past two. The script I use, which I based off a somewhat outdated tutorial, seems to be seriously flawed. However, with my admittedly limited understanding of LineRenderers, I'm not sure where the problem lies. I'm fully willing to start from scratch on this, but if this tutorial isn't working I really don't know where to start.

Since I know I'm not the only one working with reflecting lasers in this game jam, can anybody possibly point me to either where I went wrong, or point me towards a line reflection tutorial that will give me a better place to start?

The script in question: Github Link

Video demonstration:

Submitted(+1)

here you go... with this type of logic, always start with the 'end' state/condition and work backwards into the loop

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof(LineRenderer))]
public class RayCastReflection : MonoBehaviour
{
    //this game object's Transform
    private Transform goTransform;
    //the attached line renderer
    private LineRenderer lineRenderer;
    //a ray
    private Ray ray;
    //a RaycastHit variable, to gather informartion about the ray's collision
    private RaycastHit hit;
    //reflection direction
    private Vector3 direction;
    //the number of reflections
    public int nReflections = 2;
    //max length
    public float maxLength = 100f;
    //the number of points at the line renderer
    private int numPoints;
    //private int pointCount;
    void Awake ()
    {
        //get the attached Transform component  
        goTransform = this.GetComponent<Transform> ();
        //get the attached LineRenderer component  
        lineRenderer = this.GetComponent<LineRenderer> ();
    }
    void Update ()
    {
        //clamp the number of reflections between 1 and int capacity  
        nReflections = Mathf.Clamp (nReflections, 1, nReflections);
        ray = new Ray (goTransform.position, goTransform.forward);    
        //start with just the origin
        lineRenderer.positionCount = 1;
        lineRenderer.SetPosition (0, goTransform.position);
        float remainingLength = maxLength;
        //bounce up to n times
        for (int i = 0; i < nReflections; i++) {
            // ray cast
            if (Physics.Raycast (ray.origin, ray.direction, out hit, remainingLength)) {
                //we hit, update line renderer
                lineRenderer.positionCount += 1;
                lineRenderer.SetPosition (lineRenderer.positionCount - 1, hit.point);
                // update remaining length and set up ray for next loop
                remainingLength -= Vector3.Distance (ray.origin, hit.point);
                ray = new Ray (hit.point, Vector3.Reflect(ray.direction, hit.normal));
                // break loop if we don't hit a Mirror
                if (hit.collider.tag != "Mirror")
                    break;
            } else {
                // We didn't hit anything, draw line to end of ramainingLength
                lineRenderer.positionCount += 1;
                lineRenderer.SetPosition (lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLength);
                break;
            }
        }
    }
}
Submitted

Thank you so much! That logic makes so much more sense, too. That helps me a lot!

Hey I am making something similar and I was just wondering if you could explain how you made your laser look like that in the video.