Reflecting leaser beam

Hi, I’m making a game involving leasers and stuff ( How surprising D: !)
Anyways first thing that hit my mind was the line renderer , but there are few issues with the this approach.
*Collision detection will be more tricky, since the best way to do it is with rays, and the leaser it self will be moving
*It’s hard to play around with line renderers because they do not behave as simple gameObject.

Why line rederer ? Why not a simple plane or capsule ?
Well because i want to make these leaser beams reflect when they hit a wall.

BEHOLD MY AMAZING DRAWING SKILLS

Let’s imagine I’m using a Line Renderer. The visual approach of the reflection would be something like this:
-Tow line renderers one is filled 100% and the other 0% when hit a wall, the 100% lerps to 0% and the 0% lerps to 100%. But I have now idea how the collision detection will work with this.

As for the other methods i haven’t thought them through very well.

You use raycasts and raycasts can give you hit normals which you can use with Vector3.Reflect

As for the line renderer, you pretty much only need the one line renderer and update the points yourself, or just spawn a second line renderer.

Direction is calculated by subtracting target position from source position, swap them if it’s the wrong way round.

3 Likes

one line renderer per transform though, so if you do use more than one it needs to be on a separate child etc. I’d really just go with increasing the point count of the one line renderer and have start, middle, end :slight_smile:

1 Like

This would make the leaser movement harder since I’ll be obliged to move the points of the child separately just for the reflection transition and the whole leaser gameObject is already moving.

actually, thinking about it, you might want to consider a TrailRenderer instead, I’ve not used them too much so you might want to have a play to see if it works for you, but I think they’ll implicitly handle the intermediate points as the lead of the laser bounces around.

1 Like

you can do it with just one line renderer, just need to add more points to its point array.

Here is a post i have made to solve a very similar problem in the past.
Its a recursive method that can do multiple reflects with one line renderer.

using UnityEngine;
[RequireComponent(typeof(LineRenderer))]
public class TargetLine : MonoBehaviour {
    [Range(1, 5)]
    [SerializeField] private int _maxIterations = 3;
    [SerializeField] private float _maxDistance = 10f;
    private int _count;
    private LineRenderer _line;
    private void Start() {
        _line = GetComponent<LineRenderer>();
    }
    private void Update() {
        _count = 0;
        _line.SetVertexCount(1);
        _line.SetPosition(0, transform.position);
        _line.enabled = RayCast(new Ray(transform.position, transform.forward));
    }
    private bool RayCast(Ray ray) {
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, _maxDistance) && _count <= _maxIterations - 1) {
            _count++;
            var reflectAngle = Vector3.Reflect(ray.direction, hit.normal);
            _line.SetVertexCount(_count + 1);
            _line.SetPosition(_count, hit.point);
            RayCast(new Ray(hit.point, reflectAngle));
            return true;
        }
        _line.SetVertexCount(_count + 2);
        _line.SetPosition(_count + 1, ray.GetPoint(_maxDistance));
        return false;
    }
}
2 Likes

Finally, I find an answer. Last time I have asked this question a long time ago, but no one answer me a long time ago. :slight_smile:

Working :slight_smile:
NOTE: I’m using the TrailRenderer

3 Likes

I’m currently making a game similar to yours. Can you share the script?

Hello, I would love to share the code but unfortunately I lost the hard drive that contains this and many other prototypes