How can I make an AI that avoids obstacles WITHOUT using NavMesh?

So I am new to unity and i am designing a topdown 2D survival shooter. I have a very basic AI enemy programmed that will move towards the player in a straight line. If there is an object in the way, the AI gets stuck. I need to add a function that will tell him to move around the object and continue towards the player. I barely even know what I’m doing but I’ve got the basics of C# down. If you know what I could do, please show an example in C# it would be really appreciated. (I have a vague idea of using a raycast or something that will detect the closest collision in the direction of the player and change direction to avoid it, I really hope I made sense)

Well, there’s 2 different ways you can consider going about this. One is where the AI ‘cheats’ and knows the entire layout of the map. The AI then just picks what it thinks is the fastest route to its destination, and follows it. The other way is to make the AI “see” the things near it, and navigate based on those. This second option is a bit harder, but can also make games feel more realistic. For your specific case, we’re going to use a bit of both!

Outside of NavMesh, a semi-popular method you could use would be to set up waypoints. Waypoints are positions around your map that would allow any character to move to any position on the map by only walking on pre-set nodes. By moving to the closest node to your target, the player for example, you can then stop using waypoints and directly charge at the player, as nothing would be in the way. And if something gets in the way, just go back to using waypoints! The trickiest part, is deciding which route would be the quickest. This can be done by using a for loop to loop through each possible route, and seeing which one is the shortest, or by using A* with it to choose for you. The image below is a good representation of how this would work.

Now you could also do this without the AI knowing the layout of the map. This becomes tricky however, as making it function well can be difficult if your map is very active, but it has the benefit of being able to avoid non-static objects, which is a downside of NavMashes. This can be done by using several raycasts in different directions to probe the environment ahead of the AI, and it can use this data to see how far walls are from it, and then turn to avoid them. This would require your AI to “think” a bit more, however. If the raycast on the right shows its hitting a wall within a few meters of it, you should probably turn to the left a bit. If both sides are getting hit, keep going straight. And if there is something in front of you, turn whichever directions raycast has the furthest distance, and keep turning until you can go straight again. This logic works great on open maps, and fails on very closed space maps, or maze like areas. It’s best to have maps either open or closed, but not both. If it’s unavoidable, you could just change the pathfinding method when it enters specified zones, so that it can navigate them better. Below is an image of how your raycasts would look, and to help visualize how it would help navigate past an object.

90605-figure7.png

This type of AI pathfinding works great when it has states. Here’s an example for a guard that would chase a player if it sees them: At first the AI can be partolling between a few points until it see’s the player. Once it sees the player, it changes states and runs towards them. If it can no longer see the player, it would have to remember its last know location, and chase after that position to see if it can find the player. If it still can’t see the player, it’d then have to continue in the same direction, maybe at a slower speed, and continue searching until it gives up, and goes back to patrolling. A note though, when finding the player, the guard would not use the same raycasts used in pathfinding, but a larger cone for it’s field of vision. When chasing the player, the AI would automatically turn to face the player, unless an obstacle is in there way. When that happens, its pathfinding would take over, and it would turn slightly to the side until it can see the player again, and then turn to face the player.

If your project really is just as simple as turning around a circle that might get in the way, a lot of this might sound a bit too complicated for you, and having three forward facing raycasts like in the above image is probably all you really need. If this is all you need, I can write some code to get you started for it. If you need one of the other methods, I can probably find some good tutorials for you to follow!

I agree, this answer has leaked some of the insights I was looking for. Many thanks for those! I wish I had stumbled upon some general tips like these (e.g. “If it still can’t see the player, it’d then have to continue in the same direction, maybe at a slower speed, and continue searching until it gives up, and goes back to patrolling.”) — so if you happen to have more tutorial about general behavior of enemies/npcs in game, please share! ^^

[link text][1]@oStaiko ist right, that’s the way to go!

Today I have written an entirely new and free script for that. Please check the video (german, but doesn’t matter) and the video description for the script: Komplexe Gegner KI (AI) in Unity - Unity Game Engine Tutorial - YouTube

I came to the same conclusion, but i did not want to implement waypoints, because it is not necessary for my project. I just wanted a simple ai, which is avoiding obstacles and walks around corners, but without a navmesh and it has to follow the player. I have used 5 rays to solve it:
132908-rays.png
I scripted something, that gives you this result:
https://twitter.com/MrLarodos/status/1089989454704582656

Greetings,
MrLarodos

@MrLarodos Hey man! can you give me your script?, cause in trying to do it myself but i cannot achieve that, i tried a lot of things and i can avoid some obstacles in diferent directions, but in some obstacle shapes i get stuck or don´t avoid in the right direction, please i need help!!,@MrLarodos Hey man! can you give me that script, cause in trying to do it myself but i cannot achieve that i tried a lot of versions of this and i can avoid some obstacles in diferent directions, but in some shapes of obstacles i get stuck or don´t avoid in the right direction, please i need help!!

Thank you so much! This helps a lot. I already used A* but I am still having problems. The enemy gets stuck because he has a collider on his feet( Its a 16 bit overhead survival shooter game, and It is kind of birds eye but more of an angle to it so you can see the whole player/ enemy). The path is drawn from the center of him, so he will try to follow the proper path, but it is a very small line to follow. I think I am going to decrease the number of nodes, but the best thing I can think to do( and I’m not quite sure whether or not this is possible) is try to move the middle of the enemy (or his origin point) to be at his feet. This may work, but if not I think I will try the raycast method on top of it.

Thanks for all the help though! I think I just want to ask one quick question though.
Do you know if I can change the center point of the enemy to his feet (or the point that stores his xyz coordinates) that these paths are drawn from?

Anyway, thank you so much!