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,
– Owen-ReynoldsQuaternion.LookDirectioncan 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.