Getting percentage value (of x position) between 2 gameobjects within a limit


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!


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;