AI Detecting Player in Unity 2D - Tank game tutorial P8
In this Unity 2020 tutorial we will learn about creating AIDetector that will be part of our AI system. It will be responsible for detecting if the player is in range and visible (not behind an obstacle or another enemy) . This is part of the 2D Top Down Tank game tutorial series. Playlist • Top Down 2D Tank Game
00:00 Introduction
00:41 What turret AI needs
01:38 AIDetector script
07:13 Physics2D.OverlapCircle
09:06 LayerMask binary operations
12:29 Setting up collision layers
14:22 Enemy View Range Gizmo
17:03 Enemy detect itself!
18:20 Udemy course
Resources
Scripts and UnityPackages: github.com/SunnyValleyStudio/...
Full project: drive.google.com/drive/folder...
Want to learn more about making games in Unity? Check out my video courses:
courses.sunnyvalleystudio.com/
You can support me through Patreon:
/ sunnyvalleystudio
Assets used:
www.kenney.nl/assets/topdown-...
github.com/sparklinlabs/super...
Object Pool Design Pattern article:
gameprogrammingpatterns.com/o...
Join the discord:
/ discord
Пікірлер: 16
11:27 Man that looks scary. I hope there is a way to get the same functionality without using these bitwise operations. Great tutorial as always.
Hey, love your tutorials! I was wondering why we did 'target = null' at 1:57 if we could have used 'target' only. Thanks
@SunnyValleyStudio
Жыл бұрын
I can't recall exactly why but the general idea is that since we already have null type it is best to always reset the object to be null so that we can be sure that a check if( == null) won't go through with some old data present in the object.
@ApzyteGamer
Жыл бұрын
@@SunnyValleyStudio Hmmm... Yeah. But if we have set the default in the code editor and no detections for that object, and we put to null wouldn't the code crash?
Hi, great tutorial! I was just wondering if it is possible to make the enemy FOV circle visible while playing the game? So that players always know in what area the enemies can see them.
@SunnyValleyStudio
Жыл бұрын
Yes. The simplest way is to add a circle sprite to the enemy and make it transparent. Adding gaps based on the obstacles it a bit more difficult though.
At 4:39 can we use while (true) to loop WaitForSeconds and DetectTarget in the coroutine? It feels like starting the coroutine again is an endless recursion
@SunnyValleyStudio
Жыл бұрын
Yes there are many methods to perform this logic. We can restart the *coroutine* , call *InvokeRepeating()* or use a *while loop* . Coroutine and InvokeRepeating gives us an ability to add a delay (which we can later use to optimize the performance of our game by ex by calling DetectTarget for 50% of enemies in this frame and for the rest in the next frame / every X frames). Anyhow be sure to try your own code / your own ideas. Its the best way to learn 🙂
@zsolomon408
Жыл бұрын
@@SunnyValleyStudio Got it, thanks!
what can i change in code if i have problem with detection - if the player is in the fov all of code works great but if the player touch the center of enemy it isn't visible.
@SunnyValleyStudio
2 жыл бұрын
Hey! Thanks for letting me know. I don't recall the code exactly but The idea of fov is that we use Physics2d.Raycast detecting if the player is in enemy sight. What can happen is that for some reason raycas detects the enemy tank itself so maybe giving the player a separate "outside" collider that is meant to prevent driving through enemies and that is ignored by the raycas (on a separate layer) and creating a new smaller child collider object inside the player as a detectable collider would help? If you want join the discord (link in the description) and post some screenshots of what happens. I will take a look at it when I have a chance :)
@muhammadhuzaifaali3391
Жыл бұрын
same problem with me
Hello, when I set the player tank layer to "player" and enemy layer to "enemy" both tanks can't collide with each other, thanks.
@SunnyValleyStudio
10 ай бұрын
Sorry to hear that! Probably something went wrong with the Collision Matrix settings. Here is how to access it. Each checkbox corresponds to which layer can collide with which. Another issue might be that the Enemy or Trigger object has a collider that is "trigger" so it will never affect each other in terms of stopping due to collision.
How do I make it like the enemy tanks cannot detect the player when hiding in the trees?
@SunnyValleyStudio
Ай бұрын
You could make the tree to have some sort of trigger collider. I don't remember if we had some "obstacle" layer mask but basically you would make the enemy Raycast in the direction of the player and the raycast should use a LayerMask that includes "Player / Agent" and for example "Tree" layers (or the Obstacle laer). You would just add a check to see if the detected object is on the layer "tree" it means that the player is inside the 'tree" therefor not visible