Hello. I am trying to make a tower defense game and i am currently in the stage of making the zombies move. The way i am doing that is through a script that modifies the zombie's rotation and position once it hits a corner part. The problem is, the rotation the script sets is not relative, but it is absolute. Here is my script
local myZombie = script.Parent print("Myzombie Defined") while wait(0.3333) do --The server zombies update position thrice per second myZombie:SetPrimaryPartCFrame(myZombie.HumanoidRootPart.CFrame*CFrame.new(0,0,-myZombie.ZombieSpeedSPS.Value/3)) print("Zombie advances") collidelist = myZombie.HumanoidRootPart:GetTouchingParts() --get parts touching for i=1, #collidelist do if collidelist[i].Name == "BendWP" then --if part is corner part myZombie:SetPrimaryPartCFrame(CFrame.new(collidelist[i].ReferencePart.Position.X, 1.7, collidelist[i].ReferencePart.Position.Z)) -- as the zombie position update is so slow, i adjust its position to correct overshoots and undershoots if collidelist[i].IsRightBend.Value == true then --if part mandates to turn right local cframe = myZombie.PrimaryPart.CFrame -- Current CFrame myZombie:SetPrimaryPartCFrame(cframe * CFrame.new(0, math.rad(90), 0)) else--Else local cframe = myZombie.PrimaryPart.CFrame -- Current CFrame myZombie:SetPrimaryPartCFrame(cframe * CFrame.new(0, math.rad(-90), 0)) end end end end
PD: It is a model
Since we know your zombie has a humanoid, we can use pathfinding and humanoid:MoveTo()
Assuming that the script is inside the zombie, we will do:
local humanoid = script.Parent.Humanoid local pathFindingService = game:GetService("PathfindingService") local path = pathFindingService:CreatePath() --//Creates a path local goal = game.Workspace.Goal --//Swap goal with the position you want your zombie to go to path:ComputeAsync(script.Parent.HumanoidRootPart.Position, goal.Position) --//Analyze the path local waypoints = path:GetWaypoints() for k, point in ipairs(waypoints) do if path.Status == Enum.PathStatus.Success then humanoid:MoveTo(point.Position) humanoid.MoveToFinished:Wait() end end --//All done
I didn't test this since I don't have time too, but let me know if it has any errors.
For information go to: https://www.youtube.com/watch?v=VWKNtqjPKn0
Cheers, Zander