How to make a line renderer (laser beam) smoothly follow the mouse?

SOLVED

To make a line renderer or laser smoothly follow the mouse, and yet still stop at colliders without snapping to them I used to following workaround:

Using the Angrybots ‘LaserScope’ and ‘Per Frame Raycast’ scripts, I adjusted to following:

Per Frame Raycast: (Change the script into the following)

private var hitInfo : RaycastHit;
private var tr : Transform;
var LaserPoint : Transform;
var rotationSpeed = 3; 
          
function Awake () {
       tr = transform;
}

function Update () {
	// Cast a ray to find out the end point of the laser
	   transform.LookAt(LaserPoint);
   	   Physics.Raycast (tr.position, tr.forward, hitInfo);
}


function GetHitInfo () : RaycastHit {
	    return hitInfo;
}

Next, make an empty game object that will follow your mouse.

Attach the following script onto the Empty Game Object:

var DistancefromCamera = 0.00; //Preferably high amounts)
    
function Update(){
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var pos : Vector3 = ray.GetPoint(DistancefromCamera);
        transform.position = pos;
}

Now, you can just attach the ‘LaserScope’ and ‘Per Frame Raycast’ script on your object which should fire a laserbeam. (Note that you might want to use a Laserdot object, or just copy the one from Angrybots)

Assign the ‘Mouse Following Empty Game Object’ onto the ‘Laser Point’ in the inspector… and that’s about it! (Laser Point can be found under Per Frame Raycast).

I am sure this isn’t the best workaround, and the scripts might be formatted poorly, or contain flaws (as I am quite new to scripting) but, it works great for me!

Biggest possible improvement is Raycasting doesn't need any gameObjects. There are several(?) threads here on that. For example, Quaternion.LookDirection can be used instead of LookAt to figure which way gameObject A would be looking at B, if they existed (which they don't.) Sometimes "just math" ray casts can be easier to write. If you had 100 items all making an extra gameObject and lagging, might be worth changing. But for just one, don't change what works.

1 Answer

1

The code you’re looking at seems to use hit.normal to pull back a little, maybe to avoid overlapping? The problem is, the normal could just as easily move it sideways. The ray end point would have some odd jerks as you moved it over the surface – almost like a magnet was pushing it. Could remove hit.normal and see how it looks.

For a real pullback, something like: Vector3 unitRay = (rayEnd-rayStart).normalized; rayEnd -= unitRay*0.4f;. That would pull the end back 0.4 meters, without changing the aim point.

Thanks for the answer, I tried removing the normal, and it did certainly make it better, but not perfectly smooth. Your answer got me thinking however, and I found a workaround using the Angrybots laser and adjusting that script a little here and there. I will post my workaround, and regard this question answered. Thanks for your help!