Figuring out positions within a distance between two points.
Hi guys. It's kinda difficult to explain what I'm trying to achieve here but here goes.
Basically I'm trying to get the distance between two points, Get each integer within that float value and figure out the position if each integer.
In the Image I've attached, You can see there are two points set at a distance of 5. I've manually placed markers at each 1 unit along the line. It's these positions of each marker I'm trying to figure out.
So for example, in this instance, Once pointA and pointB have been positioned, A collectable object can be placed at marker 1,2,3,4 and pointB position.
I'm unsure if I'm even approaching this in the right way.
I hope I've explained this clearly. Any advice on how to approach this would be very much appreciated.
Thanks
Answer by Hellium · May 02, 2018 at 08:56 AM
I haven't tested the following script, but I think it's a good start:
public Vector3[] GetCollectablePositions( Vector3 pointA, Vector3 pointB, float distance )
{
// The direction vector between A and B
Vector3 direction = pointB - pointA ;
// Get the number of collectables
int collectableCount = Mathf.Floor( (direction.magnitude - Mathf.Epsilon ) / distance ) ;
// Create the array of positions to gather all the positions
Vector3[] collectablePositions = new Vector3[collectableCount];
// Direction is now a vector separating two collectables
direction /= collectableCount ;
// Fill the array by determining the positions of the collectables according to the direction vector
// and the position of A
for( int collectableIndex = 0 ; collectableIndex < collectableCount ; ++collectableIndex )
{
collectablePositions[collectableIndex] = pointA + direction * (collectableIndex + 1) ;
}
return collectablePositions ;
}
Thank you very much! After a few hours of reading into $$anonymous$$athf.Epsilon and $$anonymous$$athf.Floor as I've never used these before, I've finally got it. Thanks Again !
Your answer
![](https://koobas.hobune.stream/wayback/20220612161901im_/https://answers.unity.com/themes/thub/images/avi.jpg)