Making enemy AI detect the player in Unity 2D

In this tutorial I will show you different ways to create AI sensors so that your enemy AI can detect the player. Scripts will be about Unity 2D but the same method can be applied to Unity 3D.
Want to learn more about Unity 2D and to improve your coding skills? Check my video courses:
courses.sunnyvalleystudio.com/
You can support me through Patreon:
/ sunnyvalleystudio
Join the discord:
/ discord
Documentation:
docs.unity3d.com/ScriptRefere...
docs.unity3d.com/ScriptRefere...
0:00 Introduction
0:16 Theory
1:17 Using BoxCollider2D
3:01 UnityEvents to make it more reusable
4:47 Using an OverlapBox cast
9:03 Example of multiple detectors working together
9:48 Melee range detector using OverlapCircle cast

Пікірлер: 32

  • @nosinfantasia
    @nosinfantasia2 жыл бұрын

    very useful thanks you so much

  • @X_Daniel466
    @X_Daniel4662 жыл бұрын

    Nice just like your courses.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Thanks a lot!

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

    This is very useful. Thank you!

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Glad to hear that!

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

    This is quite excellent. I'm trying to use this and it works well to a great extent. I'm just unclear as to when PlayerDetected should be set, because this code just depends on Target. Also, I'm glad to learn you can set properties based on lambda expressions, but I'm not seeing where DirectionToTarget is used, but I trust it's controlling the enemy's direction. This is the first video of yours that I've seen, so I'm guessing the others talk more about the AIBrain and AISensor you alluded to and how they might control those properties.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Thanks! Yeah the full explanation is in my video course. If you can't / don't want to grab it you can also learn a lot about Ai from my Context Steering Ai series about how to detect objects kzread.info/dash/bejne/pn2apZJpcqicqps.html

  • @Kopellis
    @Kopellis2 жыл бұрын

    Great video. I use it in my games thank you

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Glad to hear it. Thanks for watching!

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

    thank you very much, very inspiring!

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    You are so welcome! 🙂

  • @random_precision_software
    @random_precision_software2 жыл бұрын

    Always interested in better enemy ai !

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

    time to copy type all that code :V

  • @fredericobrink259
    @fredericobrink2592 жыл бұрын

    Nice

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Thanks

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

    At the 9:20 mark in the video, it is mentioned that the enemy "won't chase us because we are behind an obstacle." Do you have any videos that can explain this AI concept? I am trying to implement it into my game! Thank you!

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    I explain it a bit in my context steering ai tutorial kzread.info/dash/bejne/pn2apZJpcqicqps.html I hope it helps! 👍

  • @leehyun8174
    @leehyun81742 жыл бұрын

    Woooowwww

  • @user-ii3uq6qn8k
    @user-ii3uq6qn8k7 ай бұрын

    What does the line below do? Does it return a value of target's position - detectorOrigin's position and what's its use cause i don't see it being referenced anywhere. public Vector2 directionToTarget => target.transform.position - detectorOrigin.position;

  • @SunnyValleyStudio

    @SunnyValleyStudio

    7 ай бұрын

    It appears to calculate the direction to the target (I would suppose to the player). The naming "detectedOrigin" is because our collider detected by the enemy might not be placed at the center of the Player (where the hit collider is). If we don't adjust for that we can have the enemy attacking in the direction say "above" players head which can cause bugs. In general this line is meant to calculate in which direction the enemy should move to reach the player.

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

    Thank you and How can I get sources from this video?

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Hey! This is from my *Make a Juicy video course* so to get the code and the full project for this and more consider getting the course courses.sunnyvalleystudio.com/

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

    This video is so useful. But I have a problem. I will be very glad if you can help. I'm trying to use same method on Unity3D. The problem is Gizmo doesn't match with detector size. Detection happens and gizmo turns into red(which is detection color) before player goes into gizmo.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    In general the gizmo is driven by our code. There is a chance that there is a bug in the code. Often the issue is that the [SerializaeField] has some default value in the script but because we have changed it in the inspector it is causing the inconsistency. Maybe some detection value is use for the detection check but some other value is used to draw gizmo?

  • @user-ey4lf2mp6z
    @user-ey4lf2mp6z6 ай бұрын

    I want to know what gun sounds in the game are

  • @SunnyValleyStudio

    @SunnyValleyStudio

    6 ай бұрын

    I don't recally what it was exactly but I have found it in the freesound.org/ cc0 section and boosted its bass / treble in Audacity to make it sound better 🙂

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

    its very nice :D can i make the Gizmo box flip when my ai turn around?

  • @Thrymbor

    @Thrymbor

    Жыл бұрын

    nevermind i did it :D

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Great to hear that!

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

    Did everything! What about the animations though?

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Hey! I explain animations, shaders and other topics in my courses courses.sunnyvalleystudio.com/ if you want to learn more 🙂

  • @glitcherL00ns

    @glitcherL00ns

    Жыл бұрын

    @@SunnyValleyStudio Thanks! Does it also link to this video