How to make humanoid move back from an enemy

hi community.

i am trying to make my soldier move back from the zombie he is shooting at when it comes too near. for example magnitude < 10.

However i cant formulate a script to do the moving back. I read up CFrames and searched here. I still cant find a solution / dont understand. Hoping if someone can kindly guide me.

appreciate.

2 Likes

To move the player backwards( assuming you want it to move directly back ) from a point, you can get the directional vector of the two Positions, and then multiply it by a negative number.

For instance:

Direction = (Character.HumanoidRootPart.Position - CPU.HumanoidRootPart.Position).Unit

CPU.Humanoid:MoveTo(CPU.HumanoidRootPart.Position + Direction * -10) --the amount you want to move backwards

Hope this helps!

5 Likes

Thanks. Actually i am trying to make the soldier move back . .aka retreat while shooting. . so am trying to wrap how can i translate this to MoveTo

In that case you can try this

humanoid:Move((humanoidRoorPart.Position-enemyRoot.Position).Unit*a number here)

Or this

humanoid:Move(-(enemyRoot.Position-HumanoidRooot.Position).Unit*a number here)
1 Like

I edited my original comment with those changes. The CPU will now move backwards using MoveTo(), in correlation to the player character’s Position.

2 Likes

Thank you for the replies! i learn a new thing :slight_smile:

can i confirm something… when we talk about positions and magnitudes or the units in this example. . are we talking about 1 = 1 stud? thank you!

When we use .Unit, we are converting the directional Vector down to being based on 1, so that we can then multiply it on a stud basis. The direction stays the same, but it gets reduced down to a magnitude of 1.

1 Like

and magnitude 1 = 1 stud? thanks!

1 Like

hi . . can i trouble you for more advice. . how do i make it move left or right of the target? many thanks

You can first create a CFrame that faces the Enemy, and then use the RightVector of that CFrame, to get the directional vectors needed to move the CPU left and right.

CFrame1 = CFrame.new(CPU.HumanoidRootPart.Position, Character.HumanoidRootPart.Position)
MoveAmount = 5 --Set the move amount to a positive number to go right, and a negative number to go left
CPU.Humanoid:MoveTo(CPU.HumanoidRootPart.Position + CFrame1.RightVector * MoveAmount)
3 Likes

thank you i am learning alot from you…

does this mean i can also somehow make the humanoid move backwards while facing the enemy? like retreating . .

You sure can! Just multiply the LookVector of the HumanoidRootPart by a negative number.

like this?

Direction = (Character.HumanoidRootPart.Position - CPU.HumanoidRootPart.Position).Unit
CPU.Humanoid:MoveTo(CPU.HumanoidRootPart.Position + Direction * -10)
CPU.HumanoidRootPart.CFrame.new(LookVector*-10)

You are close, but it would probably be something more like this:

Direction = (Character.HumanoidRootPart.Position - CPU.HumanoidRootPart.Position).Unit

CPU.HumanoidRootPart.CFrame = CFrame.new(CPU.HumanoidRootPart.Position, CPU.HumanoidRootPart.Position + Direction) -- set the CPU to look towards the Character

CPU.Humanoid:MoveTo(CPU.HumanoidRootPart.Position + Direction * - 10) Move it's Position backwards

You were really close, but just get some of the CFraming a little wrong.

1 Like

You don’t need to Change its CFrame, you can do this

CPU.Humanoid:MoveTo(CPU.HumanoidRootPart.Position + Direction * - 10,lookAt)

Unfortunately you can’t do it like this, since MoveTo only has a Position parameter, without any orientation input.

1 Like

ah i see . …so this will not work . . if i understand correctly this code can only make it look at it and then move backwards while facing away from the zombie?

Before running that code, try setting the AutoRotate property of your CPU’s Humanoid to false. This might stop the CPU from rotating towards where it is walking.

1 Like

ok thank you ExcessEnergy appreciate :slight_smile:

i know this was 4 years ago, and i’ve very late but how would i make it smooth doing the backward movement thing? i tried experimenting using the code provided and other stuff but it just does not move backwards smoothly.