How do I calculate an accurate a positive distance and a negative distance

Okay im making a distance disadvantage gui for a slot game, Basically a car game and I want to know how do I calculate it accurately since if i use (position1 - position2).magnitude it will always produce a positive number so I cant find the disadvantage.
it should look like this in the example.

I have managed to find a way but its not really accurate. I used the X and Z axis to calculate it by doing

local StudsX = (PositionX1 - PositionX2)
local StudsZ = (PositionZ1 - PositionZ2)

local Studs  = StudsX + StudsZ

heres an example of it being inaccurate when I clearly had an advantage, Sorry for video quality.

I think you should try some map-based triangulation on invisible checkpoints to determine who is ahead. Because if the map is a circuit, you might have inaccuracies. Once that’s done, you can use the first checkpoint both players are facing towards and calculate their distance between.

  • If one player’s closer to the first checkpoint, they have advantage.
  • If one player’s crossed the first checkpoint and the other is still behind, the player with more checkpoints will have advantage by default and their distance will be recalculated to from player to checkpoint to player. Unless they are extremely close.
2 Likes

If you have a part that represents a starting line, and the part is pointing forwards, you could do something like this:

local advantage = line.CFrame:ToObjectSpace(character.HumanoidRootPart.CFrame).z

This works by putting the character cframe in the relative space of the starting line, and then getting just the z component of the relative cframe.

3 Likes