I’m having a little trouble trying to get my NPC pathfinding up and running properly.
Currently the main problem is that the NPC moves jaggedly from one waypoint to another instead of smoothly like it should, almost as if it is shivering or it is confused about what to do. The strange thing is that the NPC seems to navigate it’s path near perfectly when it spots the player and starts running after them. ( Also the jagged movement occurs more severely when an obstacle is blocking it’s path to the player even though it should navigate around the object. Somehow the NPC can still see the player even when an object is blocking it’s view of them ).
The code is set up so that the NPC should patrol it’s path until it sees the player (which it determines via a raycast, if anything intersects the raycast between it and the player it determines that it cannot see the player ). If the NPC can see the player it will come running after them. But if the player manages to escape it will try to find you for 3 seconds then go back to patrolling.
I’ve been trying to figure it out, but I just haven’t been able to find a solution for this problem. I would really appreciate any help or suggestions on this matter so that I can fix this problem once and for all.
I thought it might be something to do with the ‘Jog’ and ‘Walk’ animation swapping in the ‘MoveTowards’ function. Could be something to do with the speed of the character changing when these animations are swapped. I’ve fiddled around with the code in there but no luck unfortunately. The character still flickers rapidly when walking.
I’m glad to see I’m not the only one who has experienced this problem. Thanks for your comment, I’ll have another look through the code now, I think you might be right. Hopefully :P.
The problem seems to be occurring when the NPC is heading toward it’s next way point. As soon as it sees the player it chases them and the animations play as the should. If the player gets to far away or the NPC can not see them anymore it goes back to the Search for Player or Patrol routine with the Walk animation and the NPC goes back to stuttering.
The NPC uses a raycast is when it is keeping track of the player which only applies when the player is in a certain distance of it. And the only other raycast which is on the NPC itself so that it can detect if it has hit the player.
Could it still be a problem with the raycast? I just can’t figure it out.
I made a similar code as yours, although the NPC doesn´t handle obstacle avoidance, but it did not have this jagged movement. Maybe if you use splines to move the NPC between 2 waypoints solves the problem. I think Andee has a class with splines movement in one of the posts. Search the forum for splines.