How to use Context Steering Game AI in Unity - P2 Detectors

In this video we will create AI Detectors that will help us to detect the Ob stales around the enemy as well as if a Target (Player) is in Range and is visible to the Enemy.
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:
/ 68964987
𝗛𝗮𝘃𝗲 𝗮𝗻𝘆 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀? 𝗝𝗼𝗶𝗻 𝘁𝗵𝗲 𝗱𝗶𝘀𝗰𝗼𝗿𝗱!
/ discord
𝗣𝗟𝗘𝗔𝗦𝗘 support the channel:
/ sunnyvalleystudio
00:00 Project Setup
01:24 Class Structure
03:00 AI Data Script
05:07 Obstacle Detection AI
08:31 Target Detection AI
13:12 Testing the Detectors
#unity2d #ai #gamedev

Пікірлер: 21

  • @Str8Griz
    @Str8Griz2 жыл бұрын

    Implementing this now in my game! Thanks!

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

    Hey so a couple of notes if you are having problems: 1. Your player collider MUST overlap your players center transform or the detection will only work sometimes. 2. Your enemy collider can block the raycast causing problems. You want to Disable 'Queries Start In Colliders' under Edit - Project Settings - Physics 2D This will stop the raycast from counting the enemy collider that it starts in.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Thanks for the feedback! 🙂 I will try to record another video mentioning those fixes.

  • @mog955

    @mog955

    9 ай бұрын

    For the point 2, are you referring to the raycast casted from the TargetDetector? Why the LayerMask aren't enough?

  • @NeoSmooth
    @NeoSmooth2 жыл бұрын

    Thank you :)

  • @user-ig1ju2fy6u
    @user-ig1ju2fy6u Жыл бұрын

    I have a question. If I have obstacles created as a grid with TIleMapCollider2D, how to detect obstacle position? OverlapCircle just returns a single TilemapCollider2D positioned in the center of the grid. It is possible to call raycasts around all directions, but this is limited to an angel step between directions and can have low peformance.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Hey! I think in the tutorial we are using *Collider.ClosestPoint* to grab the closest point fromt the obstacle to the aget in order to go around it. You could use a CompositeCollider2D I think to ensure that a collider of a different shape behaves as a single shape (which I think should help?). I haven't tested the system with a really complex obstacle. This is the part of Game dev where either you adjust the map to work with the colliders or adjust the AI system to work with the map. I think that for the most part it should still work fine. Generally if your map is complex there is nothin stopping you from using an A* to find a general path and use Context Steering just for the avoidance of other Agents that might have there own path to follow. I hope it helps!

  • @REverbSoldier

    @REverbSoldier

    Жыл бұрын

    I had the same problem, and worked using collider.ClosestPoint indeed, instead of collider.point, that always is the center of the tilemap instead the collision point. Many Thanks!! Great videos

  • @mog955
    @mog9559 ай бұрын

    Is there a particular reason why you put AIData in a component and than you dran and drop on EnemyAI script? Would't be easier simply declare it inside EnemyAI? It could also not be a MonoBehaviour. Thanks

  • @SunnyValleyStudio

    @SunnyValleyStudio

    9 ай бұрын

    You are right! Sometimes it is easier to drag the reference (because we have the inspector in unity to do that). In a non-Unity C# project we could have it as a plain C# class and pass it in the constructor - but in unity we don't have constructors for monobehaviours so it gets tricky to ensure that you pass a reference to a plain c# class before some other script tries to make use of your MB class. An alternative is to make it into Scriptable Object but again you are assigning it through the inspector because there is no way for you to find them in the project (to the best of my knowledge) . I hope it gives you some clarification 🙂 Just go with what works for you!

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

    the Line 22 in TargetDetector script is going an error for me

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    That sounds strange. It is a default unity code calling OverlapCircle() method from Physic2D class. Maybe take a look at the GitHub repo to confirm your code with mine github.com/SunnyValleyStudio/Unity-2D-Context-steering-AI/blob/main/TargetDetector.cs

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

    I'm having a hard time getting the detector to detect the player. For some reason it isn't adding any colliders to the target list.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    The best way to figure it out is to debug the code that performs the raycast and writes to the array / list. This is because the issue is either that the code doesn't run at all or that the detection parameters needs fixing. If it is the second issue check if the layerMask matches the colliders / enemy and also if the distance / radius is set correctly. At least those are the errors that I usually see.

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

    Hi Peter, I was looking for a complete tutorial about 2d rpg but i couldn't find any on the website, is there any link? I'll be glad to buy it, you're an amazing teacher

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Hey! Right now the closest I have for a beginner/intermediate level dev is my 2D Farm Game course courses.sunnyvalleystudio.com/p/2d-farm-game-in-unity-2022 but it is still in early access (although it already has a lot of content). I also have this free playlist about some RPG mechanics in Unity if that suits you better kzread.info/dash/bejne/X5eTrruKl6qecrw.html&pp=gAQB

  • @MrOmenMusic

    @MrOmenMusic

    Жыл бұрын

    @@SunnyValleyStudio i've already finished the playlist, but i would be glad to support your works, i'm learning a lot thanks to you!

  • @lukaspetrikas6320
    @lukaspetrikas63205 ай бұрын

    Perhaps anyone else is having the same issue that the Raycast hit is null at certain angles (directly infront of the enemy or behind the enemy) ?

  • @SunnyValleyStudio

    @SunnyValleyStudio

    5 ай бұрын

    I think I have responded to it on discord! 🙂

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

    Im trying this but i cannot detect the player. In Target Detector I can Debug and find that the player is in Collider2D playerCollider but it does not go in the if statement if(hit.collider != null && (playerLayerMask & (1

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Hey! So the condition checks if the hit.collider is not null (since you have mentioned that you detect the player it probably is true - if not check the player setup if there is not another collider that blocks the raycast). The second part is all about playerLayerMask - the question here is are your sure that you have assigned the correct layer to the Player object (the object that has the collider) and if you have set the same layer in the TargetDetector component ? At 0:57 I can see that we use "Player" layer on the HitCollider object for the Player object. At 13:51 we set the layer in the TargetDetector component