Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Vector3 math - What is the difference between A - B and B - A?

I do not need help with any code. My code is function. I'm just wonder the why.

Subtracting one Vector3 from another gives you the direction. Sometimes, however, I'm not sure whether to use A - B or B - A. There is a clear difference between the two. This can be easily demonstrated when I was trying to make a robot retreat a few steps.

--Made the bot move backward, as intended.
local direction = (bot.Torso.Position - plr.Character.Torso.Position)
bot.Humanoid.WalkToPoint = bot.Torso.Position + (direction.unit*10)

--Made the bot move forward, instead of backward.
local direction = (plr.Character.Torso.Position - bot.Torso.Position)
bot.Humanoid.WalkToPoint = bot.Torso.Position + (direction.unit*10)

Aside from this example, basic arithmetic tells us that 5 - 3 will have a different answer than 3 - 5. I was simply wondering what the difference in the context of Roblox is, and when to know which Vector3 to put first.

1 answer

✓ Accepted answer

They're very simply inverses of each other. Vector3s don't have any fancy math behind them, just simple subtraction.

The general rule for "forward" (your second example) is final minus initial, or in this case, the goal position minus the current position. To go backwards, just invert it.

EDIT: As a side note, |5 - 3| = |3 - 5|. Gotta love absolute values.

This discussion is preserved as it was. New answers, comments, and votes are closed.