Hello, I am new to working on how to make an enemy for my game. The problem would be that my enemy will stop at each waypoint and then continue towards the enemy. How can I make it smoother as if it was not following any sort of line towards the player
local function findTarget()
local dis = 300
local target = nil
for i,v in pairs(game.Workspace:GetChildren()) do
local hum = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("raydetector")
if hum and torso and v ~= script.Parent then
if (ctorso.Position - torso.Position).magnitude < dis then
dis = (ctorso.Position - torso.Position).magnitude
target = torso
end
end
end
return target
end
while wait(.000001) do
local target = findTarget()
if target then
local ray = Ray.new(ctorso.position, (target.Position - ctorso.Position).Unit * 200)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
if hit then
if cHumanoid.Health <= 1 then
alive = false
end
if alive == false then
local rip = script.ded
local ded = cHumanoid:LoadAnimation(rip)
script.Parent["Meshes/Head (1)"].Sound:Play()
ded:Play()
wait(4)
ctorso.Parent:Destroy()
end
path = pathfind:CreatePath()
path:ComputeAsync(ctorso.Position, target.Position)
print(path.Status)
waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.NoPath then
cHumanoid:MoveTo(target.Position)
end
for i,waypoint in pairs(waypoints) do
print(waypoint.Position)
cHumanoid:MoveTo(waypoint.Position)
cHumanoid.MoveToFinished:wait(2)
if (waypoints[#waypoints].Position - target.Position).magnitude > 10 then
break
end
if (ctorso.Position - target.Position).magnitude < 10 then
cHumanoid:MoveTo(target.Position)
local attack = script.Attack
local fire = cHumanoid:LoadAnimation(attack)
fire:Play()
for i,v in pairs(ctorso.Parent.Damagepart:GetChildren()) do
if v:IsA("BasePart") or v:IsA("UnionOperation") and v ~= script.Parent.HumanoidRootPart then
v.Touched:Connect(function(p)
if p.Parent:FindFirstChild("raydetector") then
if ouch then
ouch = false
p.Parent.Humanoid:TakeDamage(49)
wait(2)
ouch = true
end
end
end)
end
end
wait(2.5)
ouch = true
end
end
end
end
end