Making Characters Move Away from the user


Similar to how the rolling eggs work in World 3 of the Egg Hunt, I’m creating characters which move away from the user when they get in a certain range. What I’m trying to do is multiply the magnitude of the vector between the player and the NPC characters and moving them to that position so that they’ll walk away.

Is anyone sure of how to do this? My attempts have been fairly comical.

local character1Position = NPC.HumanoidRootPart.Position
local character2Position = playerCharacter.HumanoidRootPart.Position

local positionDifference = character2Position - character1Position  -- This is the direction and distance from character1 to character2.
local normalizedDirection = positionDifference.unit  -- This is just the direction, with a distance of 1.

-- If we want character1 to move away from character2 then this would be where they want to move to.
local RUN_DISTANCE = 10  -- This variable defines how far away the target location will be from their current position.
local targetAvoidanceLocation = character1Position + normalizedDirection * -1 * RUN_DISTANCE
-- We multiply by -1 because we want character1 to run in the opposite direction.