How to use Context Steering AI in Unity - P1 Overview

Creating a Context Steering AI in Unity that makes an Enemy move naturally can be tricky. In this video we will implement Context Steering Behaviors in Unity for Chasing the player while Avoiding Obstacles to make our Enemy appear smart.
Learn C# for Unity by making games:
courses.sunnyvalleystudio.com/
Previous tutorials about this system:
• Unity RPG Mechanics
Context Steering article:
www.gameaipro.com/GameAIPro2/G...
Github Scripts
github.com/SunnyValleyStudio/...
Patreons Link - Full Project:
/ 68964987
𝗛𝗮𝘃𝗲 𝗮𝗻𝘆 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀? 𝗝𝗼𝗶𝗻 𝘁𝗵𝗲 𝗱𝗶𝘀𝗰𝗼𝗿𝗱!
/ discord
𝗣𝗟𝗘𝗔𝗦𝗘 support the channel:
/ sunnyvalleystudio
00:00 Intro
00:49 What is context steering
02:51 Weights
03:22 Interest and Danger arrays
04:49 Selecting the movement direction
06:03 Enemy Wall Avoidance Problem
#unity2d #ai #gamedev

Пікірлер: 50

  • @aledjones8816
    @aledjones88162 жыл бұрын

    Love this. I've played with A* in the past but I always wanted something a tad simpler and more lightweight, and this seems perfect. Your videos are awesome, keep up the great work!

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Great to hear! Thanks for watching 🙂

  • @nightyonetwothree

    @nightyonetwothree

    Жыл бұрын

    it's better to use A* for some chunks of a map with big obstacles (walls, relief etc) (still can use steering to move around small obstacles), and when you come in some close distance - switch purely to steering.

  • @crunchyduck
    @crunchyduck Жыл бұрын

    The idea of caching the last position has *saved* my path finding. I can't believe I didn't think of that! Thanks man :)

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Glad to hear that!

  • @alright4422
    @alright44222 жыл бұрын

    I'm glad you're doing a video on Context Steering. I've known about Context Steering for some time, but I was still always surprised at how few resources can be found out there, even though the algorithm can be really useful.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    I didn't understand it fully myself a while ago but now I can see how simple yet effective it is. I really hope that this tutorial will help others to understand it🙂

  • @daveh5139
    @daveh5139 Жыл бұрын

    I am so excited. I have been looking for something like this for months.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Glad that I could help! 🙂

  • @laniakeadev.2271
    @laniakeadev.22712 жыл бұрын

    Thanks, your tutorials are brilliant among others on Unity & Gamedev, and offer good codestyle

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Happy to hear that!

  • @mbsnke2349
    @mbsnke23492 жыл бұрын

    Awesome stuff, thank you so much for this amazing tutorials!

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Glad you like them! Thanks for watching 🙂

  • @MarekNijaki
    @MarekNijaki2 жыл бұрын

    Awesome topic

  • @yairtayb8665
    @yairtayb8665 Жыл бұрын

    your tutorials are on point, thanks for your guidance

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Glad you like them!

  • @dappercthulhu4386
    @dappercthulhu438611 ай бұрын

    Hello, I'm not using unity for my project (using godot), but your explanation of this algorithm is so succinct that I was very easily able to translate this into an entirely different engine. Thanks!

  • @SunnyValleyStudio

    @SunnyValleyStudio

    11 ай бұрын

    Thanks! I'm really glad to hear that 🙂

  • @TheKr0ckeR
    @TheKr0ckeR2 жыл бұрын

    Incredible explanation! Thanks.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Thanks for watching! I hope that you find context steering helpful 🙂

  • @gamedevbaiyi936
    @gamedevbaiyi9362 жыл бұрын

    This's what I need !!! I'm coding my own Pathfinding System, but pathfinding to a moving item is very costly. This is a new solution for me. Thank you bro !!! Have a good day. 😆😆

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    I'm glad to hear that! Thanks for watching :)

  • @tucccci
    @tucccci2 жыл бұрын

    Very good explanation! Looking forward to the code.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Thanks! All the parts are now available 😉

  • @Synith19
    @Synith19 Жыл бұрын

    I think this will be the solution I need for my current problem, thank you!

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    It is a pretty nice system. My implementation might require some extension (for enemies in groups to avoid each other) and maybe some edge cases needs to be addressed but I think It should give you a nice base for Enemy AI system 🙂

  • @Synith19

    @Synith19

    Жыл бұрын

    @@SunnyValleyStudio I'm currently moving enemies directly towards player and to prevent stacking I'm having each enemy check its position against every other one and moving away if within a certain range. Was thinking I could use your system and set the other enemies as "obstacles" to be avoided while trying to follow the player. Do you think this would work well for that?

  • @RyuBateson218
    @RyuBateson2182 жыл бұрын

    Thanks!!

  • @raghavgohil2004
    @raghavgohil20048 ай бұрын

    I loved this video. thanks

  • @SunnyValleyStudio

    @SunnyValleyStudio

    8 ай бұрын

    Thanks for wathing! 🙂

  • @masterlewzilla2116
    @masterlewzilla2116 Жыл бұрын

    I love your tutorials so much! Im very lucky to found you on youtube! I have one question. How can i make that the enemy dont stack at each other? and spread around the player to attack.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    You can add to the enemy another behaviour to avoid each other (like in a flocking algorithm)

  • @ariyabayat5292
    @ariyabayat52922 жыл бұрын

    Thanks so much for this tutorial, I really enjoyed it! You managed to make context steering sound simple! 6:03 About the enemy wall avoidance problem, do you think there's any way of getting past this with another solution? Testing this system out with complex environments like moving obstacles, the last cached position can end up at the other side of a moving obstacle so the enemy tries to run at it until the obstacle moves away. I was thinking about maybe somehow mixing pathfinding with this system. Do you have any suggestions?

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Hey! For a more complex environment you would need a pathfinding algorithm. It all depends on what you want to have in your game. You could just implement a search behaviour where when the player is gone you search for it and you have a behaviour with a 50% chance of pointing the enemy to players position by using the result of the a* to lead the enemy to the next position on the path to the player (its just an idea).

  • @ariyabayat5292

    @ariyabayat5292

    2 жыл бұрын

    @@SunnyValleyStudio I see that makes sense. I'll Experiment with this and see how it goes. Thanks for your help!

  • @mehdikovic
    @mehdikovic Жыл бұрын

    Hello there, thank your for your awesome tutorial. I just wondering, what is the performance penalty for using 8 (or 5) raycasts instead of navmesh baking and querying A* path in every update if any? Using raycasts and breadcrumbs is awesome

  • @mehdikovic

    @mehdikovic

    Жыл бұрын

    I am talking about at least 100 enemies who seek and steer towards the Player simultaneously

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    I'm not really sure. You can always optimize the code, batch run collision detection every X milliseconds for parts of the enemies (you often would see it in RTS when not all unitys move at once). I don't really get that much time to test the performance of the code. I will want to tackle the Profiler / performance analysis in unity in one of the tutorials 🙂

  • @kiddo7357
    @kiddo7357 Жыл бұрын

    How to make this work with a tilemap collider instead of smaller, individual obstacles? I tried to follow this series but the "obstacles" array can only have the tilemap collider, not the colliders on the tiles themselves.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    I think the problem is that we are using "obstacleCollider.ClosestPoint" because we know that the colliders are separate. Instead of the list of colliders you could save the RaycastHit2D reference from the Raycast that you shoot (modify the Detector code to use a RaycastHit2D[]) and this way you can get the RaycastHit2D.point which ensure that you will get the closes point in ex UP direction instead of the RIGHT (because that tilemap collider is the whole thing and the one on the RIGHT is closer). docs.unity3d.com/ScriptReference/RaycastHit2D-point.html Does that makes sense? I haven't tried it but that is how I would try tackling it.

  • @kiddo7357

    @kiddo7357

    Жыл бұрын

    @@SunnyValleyStudio Still, just to make sure that I understand it correctly, at specific intervals, our enemy will fire a ray in each of the 8 directions to detect obstacles instead of using overlapcircleall, is that what you meant?

  • @peche87_gameDev
    @peche87_gameDev2 жыл бұрын

    How you manage more than one enemy if they should avoid between them? I think a good answer would be flocking or flow field… but I would like to see what you think. Great content :) Thanks!

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Yeah all you need is to add the Steering behaviors for the enemies to avoid each other (maybe play with the weights to make it a low priority behavior). Definitely you could add flocking steering behaviors to this system to make it work .

  • @user-kx1ug9ey9o
    @user-kx1ug9ey9o2 жыл бұрын

    thanks, got pro for free

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Consider supporting me on patreon or checking out my video courses to support my work :)

  • @loot6
    @loot6 Жыл бұрын

    Seems good in theory but in practise has flaws. For example if the target is east and east is blocked by a small NPC in front of the target, just large enough to block only one ray, the best directions will be diagonal NE and SE to take you around the object. But if you take the average of those it's obviously east...which moves you into the NPC...

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Definitely. Making AI for games is a bit of science and a lot of trickery. It is just a matter of how much do you test the edge cases and how many do you patch by some custom logic complementary to the general system. in the tutorial I use breadcrumbs (well a very basic 1 breadcrumbs ) logic to give the enemy the last seen position of the player so that in the situation that you describe the enemy reaches the edge of the obstacle instead of doing what you have described.

  • @loot6

    @loot6

    Жыл бұрын

    @@SunnyValleyStudio Yes I guess breadcrumbs may help here since the target would most probably be out of sight in my example.

  • @DungHoang-ff2wh
    @DungHoang-ff2wh2 ай бұрын

    oh after i saw this vid i recognize that i am not suitable for game development

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 ай бұрын

    Me too probably but I am giving it my best shot 👍