- Home /
AI enemy Stuck at corners
I have an AI script for my enemy which has character controller: Its aim is to follow the player and It actually does But when the ai meets certa![alt text][1]in situations like what is on the attached picture: [1]: /storage/temp/13119-untitled-1.jpg It gets stucked! My question is : How can i make the enemy "stop following the player and turn" then "after turning follow the enemy"? Please help!
I tend to narrow down the actions on my algorithms. In your case it would go:
Check the distance
Check the line of sight
Check the linecast
Now if all of those go true it means the NPC is close enough, the player is front and the NPC can see the player. It still can happen that an obstacle is in between so you check the raycast.
See doing this way, you can avoid some useless action like your Linecast at the beginning.
As for rotating you only need to apply a rotation by a defined angle if the raycast hits something, if the raycast keeps on hitting then your guy will rotate until free.
Answer by DryTear · Jul 14, 2013 at 02:08 AM
A* Pathfinding is what you need : LINK
The current script your using directly follows the target no matter what, pathfinding consists of way points that allow the enemy to move by the way points to its target.
I've made a solution using Linecast... Anyway, thank you for your response! I'll try to read it and compare which solution is better.
Answer by fafase · Jul 16, 2013 at 02:03 PM
If you don't want to go into pathfinding - which is the ultimate way anyway - you can use steering behaviours.
While your guy is heading to the player, you have two raycasts on each side of the NPC and if the cast returns true, you have a hit, rotate the NPC by an angle.
It is just a basic solution that will not fix entirely the issue and there could be (will be) situations where your guy is just stuck.
But it is an easy way.
You cant blame me because I'm just a student, and i find it hard to understand the script in pathfinding... so I just solved it my own way... Actually I have 3 linecast including in front of the enemy: -The enemy keeps moving forward -rotating towards the player -if the enemy hit something in front it will rotate either left or right ins$$anonymous$$d of rotating towards the player until the cast in forward disappeared -if the cast in forward disappeared it will continue ai$$anonymous$$g towards the player Can you help me shorten my code?