- Home /
How to segment a number line of game objects between two points dynamically?
How can I dynamically segment a line between two points? For example, I want to put GameObjects between two points and as more objects get added, I want to segment the conceptual line by a number so I can have a multiple to separate the objects evenly. How would I go about it? Here is the code I was working with:
float distance = 0;
int numOfObjects = 1;
float segment = 0;
//Get the distance between two limits (left and right)
distance = Vector3.Distance(leftSide.transform.position, rightSide.transform.position);
//segment = leftSide.transform.position.x + rightSide.transform.position.x / numOfObjects;
numOfObjects = numOfObjects + 1;
float dist = Mathf.InverseLerp(rightSide.transform.position.x, leftSide.transform.position.x, distance / numOfObjects);
//float dist = Mathf.InverseLerp(rightSide.transform.position.x, leftSide.transform.position.x, distance);
//Set the position
someObject.transform.position = new Vector3(dist * numOfObjects, rightSide.transform.position.y, rightSide.transform.position.z);
I know that this is more of a math question, but any help would be appreciated.
Answer by kingcoyote · Jul 03, 2015 at 12:05 AM
You probably want to use Vector3.Lerp rather than Mathf.InverseLerp.
someObject.transform.position = Vector3.Lerp(
leftSide.transform.position,
rightSide.transform.position,
thisSegment / (segments + 1)
);
Ok, I was playing around with the code above and trying to make sense of it. It sort of works, but only for a debug thing I created...
//Draw a test cube
Gizmos.color = Color.red;
segmentDebug = amountDebug;
if(segmentDebug == 1)
{
segmentDebug++;
}
offsetDebug = distanceDebug / segmentDebug;
distanceDebug = Vector3.Distance(leftSide.transform.position, rightSide.transform.position);
for(int i = 0; i < amountDebug; i++)
{
Gizmos.DrawCube(Vector3.Lerp(new Vector3(leftSide.transform.position.x + offsetDebug, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, i / (segmentDebug + numOfObjectsDebug)), new Vector3(1, 1, 3));
}
Why does the above code work as expected (gets separation correctly), but the one below does not work? (I will put that in a separate comment)
if(segment == 1)
{
segment++;
}
//Get the distance between two limits (left and right)
distance = Vector3.Distance(leftSide.transform.position, rightSide.transform.position);
offset = distance / segment;
//segment = leftSide.transform.position.x + rightSide.transform.position.x / numOfObjects;
//float dist = $$anonymous$$athf.InverseLerp(rightSide.transform.position.x, leftSide.transform.position.x, distance / numOfObjects);
//float dist = $$anonymous$$athf.InverseLerp(rightSide.transform.position.x, leftSide.transform.position.x, distance);
//Set the position
//someObject.transform.position = new Vector3(dist * numOfObjects, rightSide.transform.position.y, rightSide.transform.position.z);
//someObject.transform.position = Vector3.Lerp(leftSide.transform.position, rightSide.transform.position, numOfObjects / (segment + 1));
//someObject.transform.position = Vector3.Lerp(leftSide.transform.position, rightSide.transform.position, numOfObjects / (segment + numOfObjects));
//Gizmos.DrawCube(Vector3.Lerp(new Vector3(leftSide.transform.position.x + offsetDebug, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, i / (segmentDebug + numOfObjectsDebug)), new Vector3(1, 1, 3));
//someObject.transform.position = Vector3.Lerp(new Vector3(leftSide.transform.position.x + offset, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, amount / (segment + numOfObjects));
//someObject.transform.position = Vector3.Lerp(new Vector3(leftSide.transform.position.x + offset, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, amount / (segment + 1));
for(int i = 1; i <= amount; i++)
{
//someObject.transform.position = Vector3.Lerp(new Vector3(leftSide.transform.position.x + offset, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, numOfObjects / segment);
someObject.transform.position = Vector3.Lerp(new Vector3(leftSide.transform.position.x + offset, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, amount / (segment + numOfObjects));
}
In your second block, line 20, you are setting up a for loop, iterating i. But you never use i inside your for loop. Is that intentional?
Another thing that you may not be aware of, with Lerp, the third parameter is a value between 0 and 1. I see some times where you are calling Lerp in a way that the third value is likely not between 0 and 1.
With Lerp, you give it two points (your leftSide and rightSide) and a value, between 0 and 1, and Lerp will return a point between your two points.
If you want 2 items evenly spaced between leftSide and rightSide, you would do this:
var firstPos = Vector3.Lerp(leftSide.transform.position, rightSide.transform.position, 0.33f);
var secondPos = Vector3.Lerp(leftSide.transform.position, rightSide.transform.position, 0.66f);
The 0.33 and 0.66 are 1/3 and 2/3. You have 2 objects, and 3 segments. If you have 8 objects and 9 segments, you would use 1/9, 2/9 ... 8/9.
Thanks for helping me even if I may not be good at the math side of things, I will tinker with the code (and the advice) a little bit more and will get back to you with another comment if things don't turn so well (or if they do turn out well). As for the for loop being intentional, I think it was, but I need to utilize the index number "i", but I am working on it.