- Home /
LineRenderer - Setting Max Distance
Hello,
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.
Answer by ebogguess · Jul 02, 2013 at 08:41 PM
You can clamp a Vector3's magnitude with ClampMagnitude
lineDistance = Vector3.ClampMagnitude(lineDistance, 10);
So I've done this:
var lineDistance = hittingForever.point;
lineDistance = Vector3.Clamp$$anonymous$$agnitude(lineDistance, 10);
lineRenderer.SetPosition(1, lineDistance);
This doesn't work correctly, the hit location seems to always be World 0.
It's almost there:
var lineDistance = muzzleShot.transform.position - Vector3.Clamp$$anonymous$$agnitude(hittingForever.point, 10);
This makes the Line Renderer max distance of 10, but the hit destination is way off
Your answer