This is a fantastic solution to this problem, but if you don't know Unity it can be a bit hard to understand. The main part of the algorithm is basically this: - Use a large round collider to find cover objects; these will have to be tagged with a separate physics layer to differentiate them from regular environment colliders - Filter the results; set minimum distance from the enemy the NPC is hiding from, check minimum cover height - Sort the results by distance to make sure the NPC picks the nearest cover - Use the navmesh to find the point nearest to the cover - Get the nearest navmesh edge (obstacles are always surrounded by them) to that point. The normal vector of this edge lets us check the angle towards the enemy, which tells if we're on the good side of the cover - If not, sample another point some distance in the direction opposite to the enemy and repeat the navmesh edge step. Repeat a few times if you have large cover objects. - Move to the found cover spot This can be used with enemies, grenades and probably more, although you will have to make the NPC choose what's the most important thing to hide from, or expand the cover angle check to test for several objects.
@@LlamAcademy um i cant make it work i test githıb version it works but icant make it work i am sure its about trigger and collision cuz script and layer and layer phsics are set corretly im sure also it would be cool if enemy had better path finding like choose safer path not the sortest
it need a ridigbody for triggercollider chech inever knew that but it some times bugged out i look that target transdorm reference went missing when it buggs why it went mising ?
I can't believe you did this! man you are awesome, it's even better to know that we can combine this with any type of game. Thanks for another tutorial.
I got it working. Using the navmesh normal is pretty neat, never would have thought of that. One thing I'm going to add is a raycast from the test hit point back to the target (player) to double check that the agent won't be seen from that position.
Thanks for these. I've been having a heck of a time getting nav mesh agents to work, with root motion and some various AI logic. We'll see if I can get it working!! :D
Can you cover how you would add this to the state machine? Do you need two separate line of sight checks, or can you incorporate the line of sight checking into the enemy line of sight checks from part 11? How could you have the Enemy pop out from hiding/cover to shoot at the player and then go back to hiding/cover?
This is a highly valuable tutorial. Regarding inserting this as part of a state machine, I haven't been able to figure out how to deactivate the EnemyMovement script component (i.e, for when not in use). Or should a boolean be used at a certain place in one of the script to enable/disable the hide/take cover action? If so, where? :) Thanks!
Thank you 🙏! For your question about integrating it with a state machine - the Hide behavior starts based on OnGainSight. Because of this, depending on how your state machine is set up, you have a few options. 1. You could have your state machine provide to this script a boolean value of whether they should be able to hide or not. Then in HandleGainSight you can consider this boolean and check it every so often (every frame, every X seconds, something like that) and once it's true, start this hide behavior. 2. You could have your state machine handle starting this hide behavior when it transitions to the new "Hide from player" state. In this case, you could just toggle this script AND the LineOfSightChecker GameObject enabled/disabled as the state changes to/from "Hide state" to any other state. You may need some slight adjustments to handle OnEnable/OnDisable but that should get you on the right track at least. I'm sure there's more options but these are the first two that came to mind!
Fucking sick ass content dude. Packed with info but really nice and consise with out any useless fluff. The high level overview in the intro explaining the goal of the video was also well done. Looking forward to finishing work so I can check out your you channel properly.
I have not made a cover shooting video, but if you're using the NavMesh - you can use the same concepts from this video to pick the spots and manage the AI state going into a "take cover" state then a "shoot player" state.
Hey man, love your videos especially this one. i just wanted to ask how you could implement a way for the AI enemy to sneak up behind the player, for like a first person horror game, but know where player is looking. anyway thank you again for this video.
Ive used so many of your videos for my game but I have two floors for this and even when my character leaves he just freezes so I have no clue I hope someone can help cause this is really annoying and I have little to no experience making ai. UPDATE: I moved him to my second floor which dosen't have a roof and it worked fine but if there is a roof involved it bugs.
Wow what an amazing videos, all your videos are really logical and problem solving. Sir this really helps a lot of game developers who are working on the AI type game. We really appreciate your efforts. Sir will you please make an another video on the problem I am facing. The problem is that let suppose we have a scene in which there is some nav mesh agents, they are moving randomly from one place to another. But sometimes two agents moving in opposite direction, they do not navigate properly when they collide face to face, they force each other to go first to there destination and get stuck for some short time. And this think really looks dummy AI agents. How we can improve this. Once again thanks you for you efforts sir.
Thank you! For agents not running into each other - so far I haven't found a reasonable way to make them avoid each other using the Navigation System alone. I'll keep researching it to see if I can find a good solution and if I can, I'll definitely share it in the form of a tutorial!
@@LlamAcademy Thank you so much for your honest reply sir, but one think i tell you, that you will find the solution, because i watched your videos these were really technical problems and you have solved them through your efforts. Thanks for helping us. Sir will you share your gmail with me, so i may contact you on gmail.
What if I wanted AI agents to find the furthest ones away? Currently, just inverting the comparer works except the agent gets crazy between two points and can't commit to one. Is waiting for the agent to reach his destination before taking a new decision the way to go or is there a better way? Thank you!
To choose anything except the closest one I think you would have to come up with an algorithm that weights the decision to choose a new hiding spot. How we did it here would be a good starting point, but you may need to consider things like the time since they last chose a spot, how far other options are from the current position, and any other factors that seem relevant. Using all of those you could smooth out the jitteriness you're seeing with the raw just "always go to the farthest point" selection.
Thank you! What I ended up doing is adding a boolean param that allows changing from where we calculate the cover. In this case, I allow it to be from the player's position, and inverting the comparison allows the agent to effectively find the furthest viable cover from the player
How would i go about allowing the enemy to shoot while in cover? I already have an attacking script but at the moment the position the enemy ends up in makes it wierd when it shoots. Also I how can i make it so when a piece of cover is to short, it would play an animation instead
For the first one, AI Series part 11 might help you where it covers how to implement a State Machine. For the second, probably the simplest solution would be to define a height that classifies the object as “short” and Raycast from that height a short distance to see if you hit a wall, if so, it’s too tall. If not, it’s short and you can shoot over it!
I'm forgot to update my agents stopping distance since I had it set to like 8 before I implemented this and was wondering why the agent would just stop moving to cover and stand out in the open that was why!
I am not sure that cosine is doing what you think it is doing here. the cosine of 360 is -0.284. according to your description should it not be -1 in that case? I think what you really want is (-fieldOfView +90) /90 and then limit the fieldOfView to 180. Where 180 is directly behind the hider.
Ahh yes I see now. I tried this but was still operating on the idea that 360 should be 0 so I didn't see it as right. Tho past 180 degrees it does break. Setting it to 360 is the same as setting it to 0. Also my method produces a linear interpolation whereas the cos method produces a smoothstep curve.
Liam, ur videos are great, and they provide some intermediate level programming, try considering giving them a suitable title and they might reach more people
I think somebody asked how to do it and I spent some time fiddling with it to figure out how to do this one. It's been a while so I don't remember exactly. In most cases I get an idea from somebody (another youtube video, a suggestion, or even a game), then with whatever I know today and some research I'll play around with it until I get something that works.
That awesome Dude , Now I implemented this Mechanism to My Game state machine enemy ai its working when When I copy Ur Same Walls paste into my project, Otherwise My Own Primitive Cubes and Other mesh Doesn't detect edge , it failed even 2nd Attempt even i try to change same meshcollider box collider works as well ,even my primitives baked by navmesh well and Correct layer and also i try to resize Ur walls it still work ,so i just try to copy that probuilder mesh filter component to my primitives now enemy just ignores those objects No more error i dont know whats happens background Edit: i fixed it By Making that Walls Position y = 0;
@@LlamAcademy you mean the layers? dont know what you mean by physic layers, i made a video showing how is my project ruclips.net/video/G4yJo7y4UBY/видео.html
@@godsunchainedgames2667 I mean the physics layers as mentioned towards the beginning of the video: ruclips.net/video/t9e2XBQY4Og/видео.html From your video I see that is not the problem. However, there are many missing components, I wonder if you have added the Navigation Components? I will check it out in more detail when I get home.
this channel is pure dank fire
This is a fantastic solution to this problem, but if you don't know Unity it can be a bit hard to understand. The main part of the algorithm is basically this:
- Use a large round collider to find cover objects; these will have to be tagged with a separate physics layer to differentiate them from regular environment colliders
- Filter the results; set minimum distance from the enemy the NPC is hiding from, check minimum cover height
- Sort the results by distance to make sure the NPC picks the nearest cover
- Use the navmesh to find the point nearest to the cover
- Get the nearest navmesh edge (obstacles are always surrounded by them) to that point. The normal vector of this edge lets us check the angle towards the enemy, which tells if we're on the good side of the cover
- If not, sample another point some distance in the direction opposite to the enemy and repeat the navmesh edge step. Repeat a few times if you have large cover objects.
- Move to the found cover spot
This can be used with enemies, grenades and probably more, although you will have to make the NPC choose what's the most important thing to hide from, or expand the cover angle check to test for several objects.
Thanks for writing up a recap! 🙂
Those little snippets you play whilst explaining what parts of the code are doing (like the dot product) are a really nice touch. Sweet video.
Thanks :) I feel like a little visualization helps to explain it better than just my words sometimes
@@LlamAcademy um i cant make it work i test githıb version it works but icant make it work i am sure its about trigger and collision cuz script and layer and layer phsics are set corretly im sure also it would be cool if enemy had better path finding like choose safer path not the sortest
it need a ridigbody for triggercollider chech inever knew that but it some times bugged out i look that target transdorm reference went missing when it buggs why it went mising ?
I can't believe you did this! man you are awesome, it's even better to know that we can combine this with any type of game. Thanks for another tutorial.
I never would have thought about checking line of sight in this way. Genius. Also, you give a very clear explanation of what the dot product is.
Thank you🙏
I got it working. Using the navmesh normal is pretty neat, never would have thought of that. One thing I'm going to add is a raycast from the test hit point back to the target (player) to double check that the agent won't be seen from that position.
That's a good idea especially if you have holes in the mesh the agent is hiding behind!
dued i cant make it work still for a room
Thanks for these. I've been having a heck of a time getting nav mesh agents to work, with root motion and some various AI logic. We'll see if I can get it working!! :D
Good luck! I’ve never used root motion with a NMA but I can imagine the troubles!
Instant subscription man, this video is absolutely insane!!! Thank you!
Awesome! Thank you 🙏
Great tutorial, that's the one I was looking for to implement in game I'm working on. Thanks a lot!
thank all that is divine for the advices on this channel - Chris, your videos are amazing :)
Thank you 🙏 I really appreciate that 🤗
Thanks dude. Amazing tutorial, explanation, and even source code.
Awesome 😎 I appreciate that
Can you cover how you would add this to the state machine? Do you need two separate line of sight checks, or can you incorporate the line of sight checking into the enemy line of sight checks from part 11? How could you have the Enemy pop out from hiding/cover to shoot at the player and then go back to hiding/cover?
Great work thank you very much.
hey bro thanks for you work really ☺☺☺☺
This is a highly valuable tutorial. Regarding inserting this as part of a state machine, I haven't been able to figure out how to deactivate the EnemyMovement script component (i.e, for when not in use). Or should a boolean be used at a certain place in one of the script to enable/disable the hide/take cover action? If so, where? :) Thanks!
Thank you 🙏!
For your question about integrating it with a state machine - the Hide behavior starts based on OnGainSight. Because of this, depending on how your state machine is set up, you have a few options.
1. You could have your state machine provide to this script a boolean value of whether they should be able to hide or not. Then in HandleGainSight you can consider this boolean and check it every so often (every frame, every X seconds, something like that) and once it's true, start this hide behavior.
2. You could have your state machine handle starting this hide behavior when it transitions to the new "Hide from player" state. In this case, you could just toggle this script AND the LineOfSightChecker GameObject enabled/disabled as the state changes to/from "Hide state" to any other state. You may need some slight adjustments to handle OnEnable/OnDisable but that should get you on the right track at least.
I'm sure there's more options but these are the first two that came to mind!
@@LlamAcademy Splendid, and thank you very much for your detailed suggestions (I decided to use no. 2 for the moment).
Fucking sick ass content dude. Packed with info but really nice and consise with out any useless fluff. The high level overview in the intro explaining the goal of the video was also well done. Looking forward to finishing work so I can check out your you channel properly.
Thank you! I'm glad you like the format and the content!
Wow, that's a golden tutorial
This is really gold man, Can we create a cover shooting system. Have you created any video on this? Thanks for the video.It was very helpful
I have not made a cover shooting video, but if you're using the NavMesh - you can use the same concepts from this video to pick the spots and manage the AI state going into a "take cover" state then a "shoot player" state.
@@LlamAcademy I will do that, I have watched lots of videos on AI but yours was real gold.
Appreciate your work, man. waiting for the next videos.
Hey man, love your videos especially this one. i just wanted to ask how you could implement a way for the AI enemy to sneak up behind the player, for like a first person horror game, but know where player is looking. anyway thank you again for this video.
Really cool tutorial! Got it working fine, the hide slider is imperfect and I couldn't find the editor script anywhere though
Also, don't you have to "if (dot < Mathf.Cos(Mathf.Deg2Rad * fieldOfView))" ?
Nice video, thank you, But I can't this. Infact I did it but when start game , character's movements are dull. What can I do?
Ive used so many of your videos for my game but I have two floors for this and even when my character leaves he just freezes so I have no clue I hope someone can help cause this is really annoying and I have little to no experience making ai.
UPDATE:
I moved him to my second floor which dosen't have a roof and it worked fine but if there is a roof involved it bugs.
Wow what an amazing videos, all your videos are really logical and problem solving. Sir this really helps a lot of game developers who are working on the AI type game. We really appreciate your efforts.
Sir will you please make an another video on the problem I am facing. The problem is that let suppose we have a scene in which there is some nav mesh agents, they are moving randomly from one place to another. But sometimes two agents moving in opposite direction, they do not navigate properly when they collide face to face, they force each other to go first to there destination and get stuck for some short time. And this think really looks dummy AI agents. How we can improve this.
Once again thanks you for you efforts sir.
Thank you!
For agents not running into each other - so far I haven't found a reasonable way to make them avoid each other using the Navigation System alone. I'll keep researching it to see if I can find a good solution and if I can, I'll definitely share it in the form of a tutorial!
@@LlamAcademy Thank you so much for your honest reply sir, but one think i tell you, that you will find the solution, because i watched your videos these were really technical problems and you have solved them through your efforts. Thanks for helping us. Sir will you share your gmail with me, so i may contact you on gmail.
Thank you :)
And yes, you can email me at support@llama.software
What if I wanted AI agents to find the furthest ones away? Currently, just inverting the comparer works except the agent gets crazy between two points and can't commit to one. Is waiting for the agent to reach his destination before taking a new decision the way to go or is there a better way? Thank you!
To choose anything except the closest one I think you would have to come up with an algorithm that weights the decision to choose a new hiding spot. How we did it here would be a good starting point, but you may need to consider things like the time since they last chose a spot, how far other options are from the current position, and any other factors that seem relevant. Using all of those you could smooth out the jitteriness you're seeing with the raw just "always go to the farthest point" selection.
Thank you! What I ended up doing is adding a boolean param that allows changing from where we calculate the cover. In this case, I allow it to be from the player's position, and inverting the comparison allows the agent to effectively find the furthest viable cover from the player
How would i go about allowing the enemy to shoot while in cover? I already have an attacking script but at the moment the position the enemy ends up in makes it wierd when it shoots. Also I how can i make it so when a piece of cover is to short, it would play an animation instead
For the first one, AI Series part 11 might help you where it covers how to implement a State Machine. For the second, probably the simplest solution would be to define a height that classifies the object as “short” and Raycast from that height a short distance to see if you hit a wall, if so, it’s too tall. If not, it’s short and you can shoot over it!
I'm forgot to update my agents stopping distance since I had it set to like 8 before I implemented this and was wondering why the agent would just stop moving to cover and stand out in the open that was why!
😆 good catch on that!
18:29 how i acn add that editor gizmo
mine is not moving somereason i litrly copy the code from github idk why
its move some times but on enemy movement player transform isnt correcct
I have a same question like you, did you solve it?
How do I enable the Editor script?
so when my playercharacter approaches the enemy, instead of, yknow, running for cover. Unity just freezes. How od i fix
Sounds like you didn’t yield return something in the Hide coroutine
@@LlamAcademy yup that did seem to be the problem. I went to your code on the github page and cross referenced it with mine and you are right thanks
I am not sure that cosine is doing what you think it is doing here. the cosine of 360 is -0.284. according to your description should it not be -1 in that case? I think what you really want is (-fieldOfView +90) /90 and then limit the fieldOfView to 180. Where 180 is directly behind the hider.
You need to be using radians, not degrees!
Ahh yes I see now. I tried this but was still operating on the idea that 360 should be 0 so I didn't see it as right. Tho past 180 degrees it does break. Setting it to 360 is the same as setting it to 0. Also my method produces a linear interpolation whereas the cos method produces a smoothstep curve.
That’s a good point. I noticed later that the FOV was about double what was expected. Thanks for the information
Can you cover ai vision cones like in commandos
I don't know about commandos specifically, but this includes the fundamentals of the enemy vision.
Liam, ur videos are great, and they provide some intermediate level programming, try considering giving them a suitable title and they might reach more people
Thank you! 🙏
why does mine hides in front of me?
Try playing with the configurations, especially the hide sensitivity
@@LlamAcademy thanks, I eventually figured out that the size of my walls were odd
Glad you figured it out!
This video made me realize I don't know anything about programming
😅 sometimes we all feel that way
@@LlamAcademy tru im boutta study this video
i lovve youu yess youu
😁
Brother, can you make tutorial for hide and seek like game horror survival.....?
That'd be a cool series, I'll consider it
One question How did U learn This😅
I think somebody asked how to do it and I spent some time fiddling with it to figure out how to do this one. It's been a while so I don't remember exactly. In most cases I get an idea from somebody (another youtube video, a suggestion, or even a game), then with whatever I know today and some research I'll play around with it until I get something that works.
That awesome Dude , Now I implemented this Mechanism to My Game state machine enemy ai its working when When I copy Ur Same Walls paste into my project, Otherwise My Own Primitive Cubes and Other mesh Doesn't detect edge , it failed even 2nd Attempt even i try to change same meshcollider box collider works as well ,even my primitives baked by navmesh well and Correct layer and also i try to resize Ur walls it still work ,so i just try to copy that probuilder mesh filter component to my primitives now enemy just ignores those objects No more error i dont know whats happens background
Edit: i fixed it By Making that Walls Position y = 0;
If this works, my life is set!
I hope it worked for you then 😃
When you add me in your hash map
I got like a million errors. Bro I wasted 2 hours for it to bug tf out😭
Doesnt work, i move near the enemy and it doesnt hide at all, just hides at start but when i move near it doesnt look for cover
That sounds like maybe your physics layers are not set up the same as we did here to detect the player
@@LlamAcademy you mean the layers? dont know what you mean by physic layers, i made a video showing how is my project ruclips.net/video/G4yJo7y4UBY/видео.html
@@LlamAcademy This is a video of how is failing to find cover for a third time ruclips.net/video/LGeRSoLfsZ4/видео.html
@@godsunchainedgames2667 I mean the physics layers as mentioned towards the beginning of the video: ruclips.net/video/t9e2XBQY4Og/видео.html
From your video I see that is not the problem. However, there are many missing components, I wonder if you have added the Navigation Components? I will check it out in more detail when I get home.
@@LlamAcademy I tried it in unity 2021 instead unity 2018 and it works more times but ends failing anyways ruclips.net/video/yOOCH_rNtGw/видео.html
The intro is a bit cringe, but the tutorial is amazing. Thanks!
😄 haven't seen a comment about the intro in a while, thanks for that