I make a line by dragging my mouse using the LineRenderer with an object at the end, but I need the line to stop slightly before the "end point" so it doesn't cover the "object" at the end.
I know I can get a point on the line using Lerp, and then use that point as the "new end position":
Vector3 newLineEndPoint = Vector3.Lerp(lineStartPoint, lineEndPoint, 0.9f);
lineRenderer.SetPosition(lineRenderer.positionCount - 1, newLineEndPoint);
But this uses a kind of "ratio", so the gap between the end of the line and my object is different depending on the length of the line.
How could I simply "substract" from the length of the line a fixed value, for example 1.5f, or even the size of my gameobject at the end of the line, and use that new Vector3 point as my "end position" for the line renderer?
CodePudding user response:
This should do the trick:
float offset = 1.5f;
Vector3 line = lineEndPoint - lineStartPoint;
Vector3 shortenedLine = line - line.normalized * offset;
if(line.magnitue < offset){
shortenedLine = Vector3.zero; // otherwise it will be inverted.
}
Vector3 shortenedEndPoint = lineStartPoint shortenedLine;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, shortenedEndPoint);