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.
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
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.
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)
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.
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.
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.