I am making a game that I need the enemy to chase a player if they were attacked, but the pathfinding service is really wonky for me and I need help.
I needed for the enemy to not go "inside" of the player, but be on a range / offset so it will be more realistic.
I tried adding offset like this in every waypoint
that was generated by the pathfinding service:
-- HumRoot = humanoidrootpart of the enemy that is chasing -- Humanoid = humanoid of the enemy that is chasing -- destination = position of the player's humanoidrootpart who attacked local range = 5 -- studs on how much distance should the enemy be to start attacking while not destinationReached do path:ComputeAsync(HumRoot.Position, destination) for _, waypoint in pairs(waypoints) do local offset = (HumRoot.Position - waypoint.Position).Unit*range Humanoid:MoveTo(waypoint.Position + offset) Humanoid.MoveToFinished:Wait() end --removed some code about computing if destination is reached end
the problem to this is, the enemy always look back / move back a step or two before reaching the destination, and sometimes they just stop. It commonly stops when there's an obstacle tho.
Any help is appreciated! Thank you!
I finally found a solution.
What I did is put an if
conditional in the waypoints array
for loop:
--waypoint_dist is the calculated distance/magnitude between waypoint position and destination for _, waypoint in pairs(waypoints) do if(waypoint_dist < range) then local offset = (HumRoot.Position - waypoint.Position).Unit * range local diff = destination + offset Humanoid:WalkTo(diff * Vector3.new(1,HumRoot.Position.y/diff.y,1)) break else Humanoid:WalkTo(waypoint.Position) end end
It doesn't exactly answer my question, but it did the same thing that I wanted to do. Hopefully someone will find this helpful too.