How To DAMAGE Enemies in Unity

Поділитися
Вставка
  • Опубліковано 3 лип 2024
  • Attacking enemies is a staple in the overwhelming majority of games.
    Learn how to setup a Damage system on enemies in just a few lines of code, and also how to use Events to broadcast messages when an enemy is killed, damaged, spawned, etc. so that your other GameObjects can react to what is going on in a clean and decoupled way.
    ✨Want to support the channel?
    Buy me a coffee ☕: ko-fi.com/bmoli
    ➤SUBSCRIBE to inflate my ego with a bigger number 💖
    ➤LIKE the video if you enjoyed, or else you're my enemy and I'll damage you
    ➤Join our DISCORD SERVER and become the worlds first trillionaire????: / discord
    Thanks for watching!
    #unity #tutorial #bmo

КОМЕНТАРІ • 63

  • @tristanlapointe9087
    @tristanlapointe9087 2 роки тому +5

    Wow thank you so much. I've never looked at basic stuff like this in such a complex yet modular and practical way. Great tutorial!

  • @bacjam6139
    @bacjam6139 2 роки тому +3

    Tutorials like this are life savers

  • @i_am_set
    @i_am_set 2 роки тому +1

    You are easily one of my favorite Unity tutorial youtubers!

  • @thenoglinman
    @thenoglinman 2 роки тому +1

    I was just about to look for an enemy damage tutorial then got this notification!

  • @taahaalbari
    @taahaalbari Рік тому

    you are awesome! Thanks for the help & Great tutorial :D

  • @godssilliestsoldier249
    @godssilliestsoldier249 Рік тому

    THIS is what i was looking for thanks

  • @T3QN1Q
    @T3QN1Q 2 роки тому +3

    based on your other recent video, "the four gamer personality types" (great vid btw) I know I'm a killer so this is what I need!

  • @michaelonuoha6548
    @michaelonuoha6548 Рік тому +1

    thank you sir you saved my entire life i havent slept since

  • @siickale
    @siickale 4 місяці тому

    thanks for the tutorial

  • @wafflesandwich7921
    @wafflesandwich7921 Рік тому

    Thank you very much!

  • @Joriku
    @Joriku Рік тому +1

    Thank you, took me hours to even get something to work. This got me able to shoot my current test enemy without issues

  • @iken_ar
    @iken_ar 2 роки тому

    Thanks !

  • @HunneProductions
    @HunneProductions Рік тому +5

    For some reason "ToList()" in the GameManager script isnt working. Do you know why?

  • @plutoniumbellamy1912
    @plutoniumbellamy1912 2 роки тому +6

    Could you make a tutorial about an ammo collection system. Say, an ammo randomly drops after a certain event occurs, then when the player collects/collides with it - it serves as an ammo for a shooting system to the enemy(s). When the ammo counter is 0, then the play can't shoot, and so on. Thanks!

  • @tuxedobird8379
    @tuxedobird8379 Рік тому +3

    i followed it but now y bullet just goes through everything i dont know what i did

    • @loozekh
      @loozekh Рік тому

      same issue. if you find a fix please lmk
      update: go to your bullet prefab and disable istrigger on the collider, it'll work then

    • @6fukraken
      @6fukraken Рік тому +1

      @@loozekh You have to add box collider (in the add component menu)

  • @Stompin40
    @Stompin40 2 роки тому +2

    Holy sh*t this was sick to see!

  • @noside6817
    @noside6817 2 роки тому

    ty king

  • @torontogamestudio2991
    @torontogamestudio2991 Рік тому +2

    Can you make a tutorial on how the next scene will load after all enemies are killed?

  • @n11ckz64
    @n11ckz64 2 роки тому +2

    Hey BMo! Can you make a tutorial about game mechanics like in "Move the block : Slide puzzle" game, please! There are no tutorials on this topic on the Internet. I will be very grateful if you make this tutorial!

  • @gnugget1015
    @gnugget1015 Рік тому +1

    it says "'Enemy' does not contain a definition for 'TakeDamage' and no accessible extension method 'TakeDamage' accepting a first argument of type 'Enemy' could be found (are you missing a using directive or an assembly reference?)", even though I wrote all the code over four times!

  • @LuanaSouza-fw1iz
    @LuanaSouza-fw1iz 2 роки тому +1

    It was nice to see a mix of direct calling a function in an script and c# events on the same structure.
    Also, when I subscribe event's on the awake/enable function it seems like many of the events are called before they are created. That's why I subscribe on start lately. Did that ever happened to you? I find subscribing on enabled and unsubscribe on disabled elegant but couldn't do reliably

    • @BMoDev
      @BMoDev  2 роки тому

      Yes, I know what youre talking about, it has to do with the order of scripts being executed - in which case the quick and dirty solution is the reorder things in your hierarchy, but you can specify which scripts execute first in the project settings menu, this way you create events first before subscribing

  • @saishsawant7410
    @saishsawant7410 2 роки тому

    wow , i was struggling with enemy destroy for so long., all the other videos were of no help ... i typed your code and a few tweak's and the code was working fine.

  • @DashzRight
    @DashzRight Рік тому

    Why the collisionEnter function is used in the projectile and not in the enemy?

  • @MaestroMarc
    @MaestroMarc 27 днів тому

    It'd be neat if you made a 3D version of this tutorial!

  • @eileeng2492
    @eileeng2492 2 роки тому +1

    Who should I test this on first?

  • @fryptompt
    @fryptompt Рік тому

    I Have an enemy prefab and i am spawning that enemy with prefab now i have health system but the health Value is common for all of the enemies clone , how can i make the health value individual for all the clones ?

  • @zendraw3468
    @zendraw3468 Рік тому

    why did you make the event in the enemies and not in the gamemanager? it shuld be the other way around

  • @jrgvsgames5673
    @jrgvsgames5673 Рік тому

    So i followed everything correctly but when i shoot my enemy it doesnt die? any help please?

  • @WladylawGomulka
    @WladylawGomulka Рік тому

    what if i have couple enemy prefabs each named diffrently?

  • @nickcapria817
    @nickcapria817 Рік тому +1

    .Count doesnt work for me any suggestions I get error CS0428

  • @LesP56
    @LesP56 2 роки тому +2

    How is this handled if the enemy is an instance of a prefab?

    • @BMoDev
      @BMoDev  2 роки тому

      When you instantiate the prefab Start will get called, but you could create a FindTarget method and call that whenever you want or it will be called in Start if you put it there like the video.
      GameObject newEnemy = Instantiate(enemyPrefab);
      newEnemy.GetComponent().FindTarget();

  • @bladeprincess
    @bladeprincess Рік тому

    7:14 should the invoke line really be after the Destroy line? how is it called if the enemy is immediately destroyed? im curious and this would really help me. thanks!

    • @bradleypetzer6802
      @bradleypetzer6802 10 місяців тому

      I'm also new to this but I believe I read recently that the Destroy action doesn't happen immediately so you are still able to call lines for a period after the Destroy. something to do with frames or updates
      Hope this helps. from a noob learning :)

  • @BlueAndy_
    @BlueAndy_ Рік тому

    Well it doesnt quite work for me, i try this exact code just in 3d and insted to check the TryGetComponent thing for an normal collison i look if my RayCast has hit an Enemy and then try to use the public TakeDamage Funcion. And it does work for the very original Enemy but not for the other clones, even if i hit the clones the original enemy dies all of the time. Idk what is wrong here really confussing.

    • @BlueAndy_
      @BlueAndy_ Рік тому

      Nevermind got it working, the only thing that i really did was assign an empty GameObject in the script name gameObjectHit and then just assined the GameObject that im hitting to this empty hitgameObject. And then i did what he in the Video did
      //this is on top of the code
      GameObject gameObjectHit;
      // this is inside the if statement when the raycast hit something
      gameObjectHit = hitInfo.transform.gameObject;
      if(gameObjectHit.TryGetComponent(out HitDetection en))
      {
      en.TakeDamage(damage);
      }

  • @contrabandest
    @contrabandest Рік тому

    I want to have a Level Complete text when the enemy counter reaches zero. How would I do that with your script? I tried the if statement but dunno what to put in the brackets ( )

  • @qdnr
    @qdnr 2 роки тому

    Спасибо

  • @travnutztv1578
    @travnutztv1578 Рік тому +1

    Lost me at the game manager part. not sure if my "using"s are correct cause you never showed the top of the script.

  • @sharkao2686
    @sharkao2686 2 роки тому +7

    private void Awake()
    {
    enemies = GameObject.FindObjectsOfType().ToList();
    UpdateEnemiesLeftText();
    }
    its giving me an error in the ToList() and the error says this Enemy[]' does not contain a definition for 'ToList' and no accessible extension method 'ToList' accepting a first argument of type 'Enemy[]' could be found (are you missing a using directive or an assembly reference?)

    • @ryanmac8984
      @ryanmac8984 Рік тому +1

      I'm not sure if this is wrong somehow, but adding "using System.Linq;" in the top of your script seems to solve this issue.

    • @cryptolotl
      @cryptolotl Рік тому

      Tried adding the using System.Linq; but still get the same error. Also tried adding using UnityEngine.UI; but still shows up. Anyone else resolve the issue a different way, or has the System.Linq worked for everyone else?

    • @Moneybefehl
      @Moneybefehl Рік тому

      @@cryptolotl im running into the same problem, ill update if i find a solution

    • @Moneybefehl
      @Moneybefehl Рік тому

      @@cryptolotl so my solution is:
      private void Awake()
      {
      enemies = new List();
      enemies.Add(GameObject.FindObjectOfType());
      }
      we first instantiate enemies with an empty list of type Enemy and then add the Enemy object

    • @HunneProductions
      @HunneProductions Рік тому

      it works

  • @Neon-no8kv
    @Neon-no8kv Рік тому

    could you use this script as a player script too
    BTW great tutorial!

  • @cank4286
    @cank4286 2 роки тому

    C# static events FTW! Unity Events and messaging suck :( . Only issue is += -= syntax is weird and causes noisy code.

  • @mlp4266
    @mlp4266 9 днів тому

    should watch

  • @fluffystuff
    @fluffystuff Рік тому

    bruh I've been scouring the internet for this damaging player and this is the only tutorial that my tiny brain can understand (bcz I'm a beginner)

  • @Unknownuncovered-NIKI7
    @Unknownuncovered-NIKI7 3 місяці тому

    broo You forgot the bullet part!!!!

  • @eggseatpegs
    @eggseatpegs 2 роки тому +2

    Yeet

  • @noahnyman4420
    @noahnyman4420 Рік тому +1

    My enemies die in 1 shot :/
    update: I die in 1 shot :/

  • @ryanem969
    @ryanem969 Рік тому +1

    I've followed this tutorial from scratch, 3 times.
    My bullets do not "destroy"
    My bullets do no damage to enemy
    What could I be missing?
    EDIT: I had "OnCollision2D" not "OnCollisionEnter2D".
    Wow.

    • @6fukraken
      @6fukraken Рік тому

      I have the same problem but i will just search other tutorial i guess

    • @ryanem969
      @ryanem969 Рік тому

      @@6fukraken Oh, I was a noob. I'm not pro now but ask me anything.