Getting percentage value (of x position) between 2 gameobjects within a limit
Hello,
In my 2D game I currently have 2 gameobjects starting at a starting point. On trigger, the 2 gameobjects will move towards each other. I'm trying to determine the % value of the distance (x positions of the 2 gameobjects) between them as they're moving towards each other. So for example, if they're pretty much on top of each other it'll be 100% and when they're at the designated starting points it'll be 0%.
I thought it would be something like gameobject1/gameobject2 but that didn't work and also didn't take into account the limits (starting points). Then i tried to follow this formula and transposed the formula to make % the subject but that didn't work as well.
I'm really not sure how to calculate this and help will be appreciated!
Cheers,
Answer by Hellium · Mar 14, 2019 at 08:58 AM
Supposing all the objects are aligned along a straight line:
float distanceBetweenStartingPoints = Vector3.Distance( startingPointTransformA.position, startingPointTransformB.position ) ;
float distanceBetweenObjects = Vector3.Distance( objectTransformA.position, objectTransformB.position ) ;
float percentage = 1 - distanceBetweenObjects / distanceBetweenStartingPoints;
Your answer
Follow this Question
Related Questions
Get distance of one axis between two objects 0 Answers
Position of empty game objects 1 Answer
randomly Instantiate items 0 Answers
Dot Product not equals 1 0 Answers
prob to define a condition to get out of a search position loop for obstacle avoidance 0 Answers