AI Static Enemy in Unity 2D - Tank game tutorial P9

In this Unity 2020 tutorial we will learn about creating a static AI enemy turret. It will rotate the turret in random direction to simulate patrolling behavior and it will shoot at the player if it is in range. We will use strategy pattern. This is part of the 2D Top Down Tank game tutorial series. Playlist • Top Down 2D Tank Game
00:00 Introduction
00:39 Recap
01:17 Creating AI system using strategy pattern
04:38 AI Behaviour abstract class
05:33 Turret patrol behaviour
08:48 Shooting behaviour
11:39 Creating static enemy
12:30 Testing
13:42 Support me?
Resources
Scripts and UnityPackages: github.com/SunnyValleyStudio/...
Full project: drive.google.com/drive/folder...
Would you like to learn how to make a 2d shooter in Unity?
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

  • @FM_GOBi
    @FM_GOBi2 жыл бұрын

    God, will Unity community ever realize what kind of resource they have in this channel? A tutorial series aimed at beginners but without god objects handling everything from input to movement to state switching with switch statements, all in one script. And to think that your channel hasn't exploded in popularity yet.

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Thanks!

  • @jaffnimanlangit5510
    @jaffnimanlangit55103 жыл бұрын

    ur amazing mate whatever u do keep up the good work mate much appreciate ur tutorial

  • @SunnyValleyStudio

    @SunnyValleyStudio

    3 жыл бұрын

    Much appreciated! I hope you will enjoy those 3 new episodes about simple AI system :)

  • @CarloToribio-yk3ch
    @CarloToribio-yk3ch2 ай бұрын

    At 10:55, line 25 in AIShootBehavior, I had to change Vector2.Angle to Vector2.Distance. Otherwise Vector2.Angle would always return 0 (probably because the angle of the turret is directed at the when it enters the field of vision Target, which makes the angle 0, 0 is always < to _fieldOfVision / 2) and always shoot. Changing it to Vector2.Distance fixed the issue. Is this a bug? did you fix that later on. Or maybe I did something wrong somewhere else?

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 ай бұрын

    I was't that great with math back than (honestly I am still improving 🙂) so it might be that i have made some mistake there. That being said I think that the Vector2.Angle was used to detect if the turret is pointing at the other object (I presume the player). That means that Vector3.distance might not do the trick for you. Its hard to explain it here without writing some code so if you want feel free to ask this on my discord discord.com/invite/RQEtYHz

  • @SunnyValleyStudio
    @SunnyValleyStudio3 жыл бұрын

    Sorry about loud noise around 1:38! I need to tweak my mic settings :)

  • @XtrminatR7

    @XtrminatR7

    5 ай бұрын

    lol its okay dude!

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

    sorry but why you set angle < 2 , does it make any sense here ? hope you can answer my question

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Hey! It is like so because we want to be sure that the enemy tank has rotated fully to face the "patrolDirection" and since we can't simply compare float == 0 due to the calculation error (if it was int we could do that but float can have some small value 0.0000001 that is rounded to 0 but a comparison will throw false) I am just assuming that if the angle is Sorry for not explaining it. I hope it helps!

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

    Hey, when Tank Enemy destroy Tank player unity will get error... How can I fix that?

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Hey! Maybe the Enemy tank has "Player" reference in its code. The easiest fix would be to modfy Enemy code to add "if(player == null) return;" . Obviously you could also load some "End Game" scene whenever player dies to also deal with the issue. Sorry about that!

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

    Hello, I really like your guide, but I have a problem. After the addition of AI, the main tank itself stopped turning the turret. I can't figure out why, everything was fine. Thanks

  • @SunnyValleyStudio

    @SunnyValleyStudio

    Жыл бұрын

    Hey! If might be that we are using prefabs / variants to reuse some parts. this causes issue that you may by mistake apply the ai script to the player tank and not to the variant of the enemy. Sorry about showing that in the beginner series. Be sue to check if when your press play in the hierarchy does your player tank have the AI script on it. If yes you must remove it by opening the player prefab and only add it to the enemy. If not chances are that something in the script is not right. You can always grab my version of the project drive.google.com/drive/folders/1cA_zaFXiW_JA8SRRgyGPBkEbYatxkqn3?usp=sharing or check the scripts here github.com/SunnyValleyStudio/Top-Down-2D-Tank-Game-Tutorial

  • @hhcdghjjgsdrt235
    @hhcdghjjgsdrt2352 жыл бұрын

    13:10 thats called loose coupling !!! awesome. How easily we destroy the parent ie static enemy instead of the child component ie tank by unityevent

  • @SunnyValleyStudio

    @SunnyValleyStudio

    2 жыл бұрын

    Most of those terms are very "abstract". *loose coupling * can be achieved in various ways - through abstraction ex Player getting interface IWeapon reference instead of specific Pistol.cs class to call *Shoot()* on it. It can also be achieved by using events. The point is that it is much easier that we can easily add new logic / substitute the existing logics without having to modify many scripts in our project. This is because we usually need to change / modify code and the project can go on for a very long time and our (now small) modifications can later mean modifying whole game - because many things now rely on your old code. That is the mindset behind it.