- Home /
Get positions at equal intervals between two Vector3
Hi!
I want to divide lineRenderer to some n parts and get vector3 positions between start position and and position. Right now I'm trying to use Vector3.Lerp for it in a for loop, but cant get the last value of lerp function right. Any help?
var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
var layerBuildMask2 = 1 << 21;
var layerCritMask2 = 1 << 22;
var combinedLayer2 = layerBuildMask2 | layerCritMask2;
var lineRenderer : LineRenderer = gameObject.GetComponent("LineRenderer");
var linePos : int = 16;
lineRenderer.SetVertexCount(linePos);
if (Physics.Raycast (ray, hit, Mathf.Infinity, combinedLayer))
{
var distance=Vector3.Distance(selectedBuilding.transform.position,hit.transform.position);
for(s = 0 ; s<linePos ; s++)
{
lineRenderer.SetPosition(s, Vector3.Lerp(hit.transform.position,selectedBuilding.transform.position,distance/s));
}
}
Answer by Owen-Reynolds · Mar 22, 2012 at 11:56 PM
Lerp expects a 0-1 percent as the last input, so just give it 0/15, 1/15, 2/15 ... 15/15. I think the confusion is that most people don't have a 0-1, so have to do math to force it. You're using lerp for the exact thing it was meant to do:
for(int s=0;s<linePos;s++) {
... lerp(start, stop, s/(linePos-1)) ...
// EX: when s=5, you get 5/15ths, so the point is 1/3rd of the way over
The extra "-1" is because you want to start at 0/15ths and end at 15/15ths (equals 100%.) That's a total of 16 points.
Eh, it's not working. It's always the same. All positions that are set are the same and the last is the pos of the target. So I am always getting a line with 15 points in one spot and the last spot is in the targetBuilding position. So no linear interpolation. If i divide the last value in interpolation by some big number(100) its getting uneven interpolated positions.
Ok, so s is int, so just change this val to some float and we are golden :D
for(s = 0 ; s<linePos ; s++)
{
lineRenderer.SetPosition(s, Vector3.Lerp(hit.transform.position,selectedBuilding.transform.position,x/(linePos-1)));
Debug.Log("s="+s+":"+s/(linePos-1));
x++;
}
x=0;
where x is a float.
Or, (don't generally use javascript) ((float)(s))
should allow you to keep s as an int, or a hack: (1.0*s)
.
Answer by Kiloblargh · Mar 23, 2012 at 05:47 PM
Divide by zero may be killing the rest of the for loop, and "distance/s" wrong.
Try this:
var targetPos : Vector3 = selectedBuilding.transform.position;
Debug.Log (targetPos);
var clickPos : Vector3 = hit.transform.position;
Debug.Log (clickPos);
lineRenderer.SetPosition(0,clickPos);
for (var s : int = 1; s < linePos; s++)
{
Debug.Log(s);
var percentage : float = s/linePos;
Debug.Log(percentage);
var splitPos : Vector3 = Vector3.Lerp(clickPos,targetPos),percentage);
Debug.Log(splitPos);
lineRenderer.SetPosition(s, splitPos);
}
Answer by farooqaaa · Mar 23, 2012 at 12:27 AM
The last value of Vector3.Lerp() smoothing. The higher its value the faster the object will interpolate to the given position. Use "0" for maximum smoothing and "1" for minimum smoothing.
If you use the form where each frame your new value is a lerp starting from your old value: value=Lerp(value, endValue, x);
then, yes, x
is the speed/smoothFactor.
But the OP is using Lerp for it's "real" use -- linear interpolation between two points.
Your answer
Follow this Question
Related Questions
How to change a value over specified time? 2 Answers
Find center between 4 points 3 Answers
Linear interpolation crash 0 Answers
Vector3.Lerp - Constant speed between distance changes 3 Answers
Coroutine or Distance Calc? 0 Answers