LineRenderer - Setting Max Distance


I’m using a Line Renderer to show the direction of my bullets (sort of like a muzzle flash). The line goes from the muzzle tip, to whatever the RayCast hits.

This all works well, but I want to set a maximum distance on my Line Renderer, of say, 10. So if my RayCast shoots beyond 10, the line renderer will shoot in the direction, but for only for a distance on 10.

And if it shoots against something within a distance of 10, then simply have the distance of the Line Renderer to be the same as the RayCast.

I was thinking about putting a max distance within the RayCast itself, but then, anything beyond 10 and it will not fire at all.

var lineRenderer : LineRenderer = GetComponent(LineRenderer);
var lineDistance = Vector3(hittingForever.point.x, hittingForever.point.y, hittingForever.point.z);

lineRenderer.SetPosition(1, lineDistance);

This is what my Line Renderer uses. Right now, it just uses the entire distance of the RayCast. I don’t know how to mathematically ‘cap’ it.

You can clamp a Vector3’s magnitude with ClampMagnitude

lineDistance = Vector3.ClampMagnitude(lineDistance, 10);