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
very useful thanks you so much
Nice just like your courses.
@SunnyValleyStudio
2 жыл бұрын
Thanks a lot!
This is very useful. Thank you!
@SunnyValleyStudio
Жыл бұрын
Glad to hear that!
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
Жыл бұрын
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
Great video. I use it in my games thank you
@SunnyValleyStudio
2 жыл бұрын
Glad to hear it. Thanks for watching!
thank you very much, very inspiring!
@SunnyValleyStudio
Жыл бұрын
You are so welcome! 🙂
Always interested in better enemy ai !
time to copy type all that code :V
Nice
@SunnyValleyStudio
2 жыл бұрын
Thanks
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
Жыл бұрын
I explain it a bit in my context steering ai tutorial kzread.info/dash/bejne/pn2apZJpcqicqps.html I hope it helps! 👍
Woooowwww
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
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.
Thank you and How can I get sources from this video?
@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/
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
Жыл бұрын
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?
I want to know what gun sounds in the game are
@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 🙂
its very nice :D can i make the Gizmo box flip when my ai turn around?
@Thrymbor
Жыл бұрын
nevermind i did it :D
@SunnyValleyStudio
Жыл бұрын
Great to hear that!
Did everything! What about the animations though?
@SunnyValleyStudio
Жыл бұрын
Hey! I explain animations, shaders and other topics in my courses courses.sunnyvalleystudio.com/ if you want to learn more 🙂
@glitcherL00ns
Жыл бұрын
@@SunnyValleyStudio Thanks! Does it also link to this video