I need to know how to make the enemy walk to the wall it hits and then turn around again like Goomba from Super Mario Bros. But only the position for X and Y?
If you just want the NPC to continually patrol from point A to point B and back again, just use a looped MoveTo:
local NPC = script.Parent -- define the NPC
local Humanoid = NPC:WaitForChild("Humanoid") --define the Humanoid
-- Create 2 parts in the workspace at the points you want to patrol to
local partA = workspace.PartA
local partB = workspace.PartB
-- use a loop to continually move to and fro
while true do
Humanoid:MoveTo(partA Position) -- tell NPC to move to Part A position
Humanoid.MoveToFinished:Wait() -- wait till it gets there (max 8 seconds)
Humanoid:MoveTo(partA Position)
Humanoid.MoveToFinished:Wait()
end
If it takes longer than 8 seconds to get from part A to part B, or it is not a direct line of sight path, then you might need use pathfinding, but there are plenty of tutorials on that.
1 Like
Hmm…, what if we did it without parts? It would probably just hit the wall and then turn around?
You would need to use raycasts.
This script I made works.
If the script’s parent isn’t the model just change local model
into your model.
local model = script.Parent
local primaryPart = model.PrimaryPart
local humanoid = model:WaitForChild("Humanoid")
local range = 1
while wait() and model.Parent do
local isHitting = false
for z = -1, 1 do
local offset = Vector3.new(0, 0, z)
local origin = primaryPart.Position + offset
local direction = origin + Vector3.new(range, 0, 0)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = model:GetDescendants()
local raycast = workspace:Raycast(origin, direction, params)
if raycast and raycast.Instance then
isHitting = true
end
end
if isHitting == true then
range = -range
end
humanoid:MoveTo(primaryPart.Position + Vector3.new(range, 0, 0))
end
1 Like
This is so perfect! Thanks you so much for helped my game! =)
No problem. I’m happy to help!
1 Like
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.