How to create a Health Life System in Unity

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 พ.ย. 2024

ความคิดเห็น • 86

  • @danielalbano6905
    @danielalbano6905 4 ปีที่แล้ว +8

    This was a great tutorial that gets straight to the point, subbed!

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว +1

      Great to have you on board 😁

  • @karenmaldonado2505
    @karenmaldonado2505 9 หลายเดือนก่อน

    I like it, i made my first thing in unity with this, only that i had to rewatch it a 100 times, bc you go really fast and there are things that i didnt notice you did bc you did it so fast that i couldnt follow, bue in the end it worked and im really happy for it, so thank you!! .I searched your game but its not longer available :(

  • @bonda4
    @bonda4 3 ปีที่แล้ว +13

    For anyone that wants to call the "TakeDamage" method on another object, remember that in the place of the "int d", you put the number of hearts that you want be destroyed.

    • @MikeCedillos
      @MikeCedillos 3 ปีที่แล้ว

      i dont get it

    • @AlexGorskov
      @AlexGorskov 2 ปีที่แล้ว

      @@MikeCedillos You define the "d" value : want to take 2 damage? d=2; 3? d=3 etc.

    • @drecandraw
      @drecandraw 2 ปีที่แล้ว

      Wicked, I needed the player to collide with a lava object to take away a life. Thanks for saving my time with this comment!

  • @ron_yyy
    @ron_yyy ปีที่แล้ว +1

    leaving a comment so it gets shown to more people

  • @PearlRoads
    @PearlRoads 4 ปีที่แล้ว +6

    Lol I thought this was gonna be something spiritual 🤣 looks like a good tutorial, I’ll save to watch later. 👍

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว +1

      Haha definitely Spiritual 🤩

  • @thananjaijongrak7604
    @thananjaijongrak7604 4 ปีที่แล้ว +2

    Awesome tutorial.
    10/10, thanks.

  • @bananasplashbro2536
    @bananasplashbro2536 2 ปีที่แล้ว +2

    i want the lifes to go away on trigger man

    • @gamedevel22
      @gamedevel22  2 ปีที่แล้ว

      You can call any function you like from within OnTriggerEnter

  • @ceciruiz4262
    @ceciruiz4262 4 ปีที่แล้ว +2

    This helped me a lot, ThankU!

  • @rainalo
    @rainalo 2 ปีที่แล้ว +1

    Thank You!! Working very well...

  • @reyk2150
    @reyk2150 2 ปีที่แล้ว

    I applied this to my project thanks to you!

  • @zandmunimdesignanddevelopm8061
    @zandmunimdesignanddevelopm8061 4 ปีที่แล้ว +1

    Thanks as always savvu

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว +1

      Hope to see this System in one of your games!

    • @zandmunimdesignanddevelopm8061
      @zandmunimdesignanddevelopm8061 4 ปีที่แล้ว

      @@gamedevel22 th-cam.com/video/5TB2ptFfdkQ/w-d-xo.html took the time to do so, and added it directly to player script with a few effects but more to come :) thanks for your Tutorials man :)

    • @zandmunimdesignanddevelopm8061
      @zandmunimdesignanddevelopm8061 4 ปีที่แล้ว +1

      @@gamedevel22 Hey man, using the destroy function I'm finding it hard to allow hp potions (as randomly generated power ups) any ideas, Instantiate(hearts[life].gameObject); throws index outta range exceptions

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว

      Hey, you could just disable and enable the gameobject when you get a potion. Store all Hearts is one Array and use hearts[i].gameobject.setactive = false or true

    • @zandmunimdesignanddevelopm8061
      @zandmunimdesignanddevelopm8061 4 ปีที่แล้ว

      @@gamedevel22 I was starting to think that(used something similar to create a wpn swap system) thanks for the advice and the line of code lol

  • @nguyendorval
    @nguyendorval 4 ปีที่แล้ว +3

    This is great explanation video. Thanks ! i was wondering if there is a way in this system to implement 'add life' as well as 'take damage' ?

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว +2

      Thank you! Yes it is possible for sure..check out one of my latest Videos about life system and potions! I think it will help you out a lot 😄

    • @nguyendorval
      @nguyendorval 4 ปีที่แล้ว +2

      @@gamedevel22 GREAT !!!! thanks for pointing this out, i found the potion video, will watch now. :)

  • @lastloony
    @lastloony 4 ปีที่แล้ว +1

    thank you, my problem is solved

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว

      Awesome nice to hear that!

  • @MrSkyglide
    @MrSkyglide 4 ปีที่แล้ว +5

    how do you make it play the destroy animation

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว +3

      First I create the animation I want and then in the Script, I replace the Destroy function with the animation.Play() function for the specific heart. At the end of the animation you can disable or Destroy the gameobject

    • @Arsvarg8
      @Arsvarg8 4 ปีที่แล้ว

      @@gamedevel22 I can't do that. I Got this error:
      "The animation state heart could not be played because it couldn't be found!
      Please attach an animation clip with the name 'heart' or call this function only for existing animations."

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว +1

      This means that you dont have an animation component on your gameobject! Assign it and then choose the animation you want to play

  • @valentinkadushkin324
    @valentinkadushkin324 ปีที่แล้ว

    Where is the "increase life" part as it says in the video comments?

  • @mauricioenvr
    @mauricioenvr 4 ปีที่แล้ว +2

    Overall it works perfect. I just have one question... how do you use the TakeDamage method in another game object?

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว

      Do you mean how to call it? The object that will collide with the Main character could call the function inside the onTriggerEnter function

    • @mauricioenvr
      @mauricioenvr 4 ปีที่แล้ว +1

      @@gamedevel22 Thanks. Casually i solved that after i posted that question. For anyone struggling with TakeDamage, is important to clarify the amount of damage the Player is going to receive.

    • @lilili765
      @lilili765 ปีที่แล้ว

      ​@@gamedevel22how do you call it like what's the code

  • @ciori1
    @ciori1 2 ปีที่แล้ว +1

    Hello! Thank you for your tutorial! Really helped. I have a question though, What could you do if you used this in a game with levels. I would use DontDestroyonLoad but if you do use that it would cause your main character to duplicate.

    • @socialskillz3780
      @socialskillz3780 2 ปีที่แล้ว

      Yes, it would cause your character to duplicate so you have to write a script that destroys the other duplicates.

  • @kuma4an
    @kuma4an 4 ปีที่แล้ว +1

    Hi, can you show me how to make random obstacle generation?
    Your way with platforms works great, Thank you !

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว +1

      Of course! It will be on my to do list for next week

  • @MirehManuh
    @MirehManuh 4 ปีที่แล้ว +2

    im having a problem with this like my first heart gets destroyed but the other 2 stay on screen

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว

      Are you sure that your Script is like the one in the tutorial?

  • @shwiftyboi7566
    @shwiftyboi7566 2 ปีที่แล้ว +1

    Anyone else getting to 2:53 and it comes up "List is empty" under hearts?
    Edit: Wait nvm, fixed it

  • @parvelqueen2398
    @parvelqueen2398 4 ปีที่แล้ว +2

    how do I move the heart image? mine is just stuck in the middle

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว +1

      Is the Image assigned to a parent gameobject? Try moving that one around or play around with the anchor points

  • @norm7798
    @norm7798 2 ปีที่แล้ว

    I cant find "Size" and whenever I typed "GameObject" it appeared white instead of green

  • @nattens6172
    @nattens6172 3 ปีที่แล้ว +1

    Thanks a lot but now how do I make my player lose a heart when he touches an enemy ???

    • @gamedevel22
      @gamedevel22  3 ปีที่แล้ว +1

      You can call the same function inside of OnTriggerEnter whenever you touch an enemy

  • @justthejust9868
    @justthejust9868 3 ปีที่แล้ว +2

    Hey, very good Tutorial thank you. But if I´m trying to call the TakeDamage Funktion on a "OnTriggerEnter2D" Function then it doesnt work. The life variable will be set from 3 to 2 like it should(I have tested it), but somehow the if statement in the Update function doesn´t recognize the change of the life variable... Do you have an Idea why ?
    PS. : when I use a button to call the takeDMG function like in the tutorial it works perfect.

    • @gamedevel22
      @gamedevel22  3 ปีที่แล้ว +1

      Hey thank you! Are you sure you are using the correct variable in the if statement of the update function? The way you call the take damage function should not be a problem even with on Trigger enter.

    • @justthejust9868
      @justthejust9868 3 ปีที่แล้ว

      @@gamedevel22 thanks for the fast answer i found the solution, i had another script that deleted the playerobject and instantiated a new playerobject on a trigger enter at a respawnpoint. That caused the integer to reset to 3 each time he got hit🤦🏼‍♂️

    • @gamedevel22
      @gamedevel22  3 ปีที่แล้ว +1

      Glad you found the solution! Keep it up :D

  • @farisuddin3529
    @farisuddin3529 3 ปีที่แล้ว +1

    What do I do if the player loses a life when it comes in contact with a spike or enemy?

    • @gamedevel22
      @gamedevel22  3 ปีที่แล้ว +2

      You can use OnTriggerEnter and use the same Code as in the video

    • @siggetiger5239
      @siggetiger5239 3 ปีที่แล้ว

      @@gamedevel22 where do i put the ontriggerenter

    • @gamedevel22
      @gamedevel22  3 ปีที่แล้ว

      I would put it in the players script and check there if he touches the spike for example by giving the right tag to the spike.

  • @thanhatnguyen8137
    @thanhatnguyen8137 2 ปีที่แล้ว

    5:32 int d on this method error because it never in used.

  • @charlottevanoers3180
    @charlottevanoers3180 4 ปีที่แล้ว +1

    how can i lose life when i fall off the platform

  • @stegobuls
    @stegobuls 3 ปีที่แล้ว +1

    Hey!
    If I press restart button in the game, how to restart my lifes?
    Thank you!

    • @gamedevel22
      @gamedevel22  3 ปีที่แล้ว +1

      Do you mean how to start with full lifes or how to continue with the amount of life from before?

    • @stegobuls
      @stegobuls 3 ปีที่แล้ว +1

      @@gamedevel22
      How to start with full lifes, exactly this :D

    • @gamedevel22
      @gamedevel22  3 ปีที่แล้ว +1

      Well at the beginning of your scene you will always have full life since this is how it's set up. Your starting life will only be affected if you save your current life in a variable that you read when starting your game over.

  • @temodeart
    @temodeart 4 ปีที่แล้ว +1

    I am having a error:
    There is no argument given that corresponds to the required formal parameter 'd' of 'GameManager.TakeDamage(int)'

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว

      In the function Parameter you will also need a name for your int variable. So you could write (int d)

  • @JackClarke91
    @JackClarke91 4 ปีที่แล้ว +1

    Hi, after pressing the button only one heart disappears, I can still hit the button but nothing happens. Tried re-adding the duplicated heat image but still doesn't work? Any help appreciated, thanks

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว +2

      Hey I may need to check your Code to find the Problem..feel free to text me on Discord 😁

  • @bayakFPS
    @bayakFPS 3 ปีที่แล้ว +1

    how can I acces void TakeDmg(in my case Takeshoot), use it as a code by other words...for example I have glob float, that checks if my player(ball) is moving, if its moving it should loose one life. I guess I should use if aswell..if (golbvalue.playerismoving){ takeshot();}...but it cant go that way..

    • @bayakFPS
      @bayakFPS 3 ปีที่แล้ว

      I managed to do it, but instead of just one heart, it destroys all heart..code is same as yours. Any idea?

    • @bayakFPS
      @bayakFPS 3 ปีที่แล้ว

      And I use no1 in ()..

    • @gamedevel22
      @gamedevel22  3 ปีที่แล้ว +1

      @@bayakFPS Glad you solved the problem! I suppose it destroyes all the hearts because the condtions of the if stameent stay always true. You can adda new boolean that is true at the beginning and add it to your if conditions. in the first line inside the if statement turn the bool to fakse. You just need to think about where you need to change it back to true in order to lose a new life later on!

    • @bayakFPS
      @bayakFPS 3 ปีที่แล้ว +1

      @@gamedevel22 thanks bro, works now!
      keep up w good work!

  • @iceyberq
    @iceyberq 4 ปีที่แล้ว +1

    Is there a way to make it so if it touches something the health decreases like spikes or an enemy

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว

      Yes of course! Just have an onTriggerEnter function and whenever you touch something that causes damage call the function of the video

    • @iceyberq
      @iceyberq 4 ปีที่แล้ว

      @@gamedevel22 I've been trying for a while to get ontriggerenter2d to decrease my health I believe my script is wrong between the {} of the ontriggerenter
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class HeartSystem : MonoBehaviour
      {
      public GameObject[] hearts;
      public GameObject Player;
      private int life;
      private bool dead;
      private void Start()
      {
      life = hearts.Length;
      }
      void Update()
      {
      if (dead == true)
      {
      Destroy(gameObject);
      }
      }
      private void OnTriggerEnter2D(Collider2D other)
      {
      TakeDamage;
      }
      public void TakeDamage(int d)
      {
      if (life >= 1)
      {
      life -= d;
      Destroy(hearts[life].gameObject);
      if(life < 1)
      {
      dead = true;
      }
      }
      }
      }

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว

      You need to call it like this..TakeDamage(1); The 1 is just an example and you can use any number to reduce your health based on that

    • @iceyberq
      @iceyberq 4 ปีที่แล้ว

      @@gamedevel22 okay thanks im not too good at this kinda stuff you helped me alot

  • @DerpieDoe
    @DerpieDoe 3 ปีที่แล้ว +1

    i get as a error that the assetbundle name hasnt been set

    • @gamedevel22
      @gamedevel22  3 ปีที่แล้ว

      That's strange..maybe it has something to do with your unity project or some assets you Imported?

  • @mangosquirrel
    @mangosquirrel 4 ปีที่แล้ว +1

    I followed all the steps but I can't click the button? Please help

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว +1

      Have you tried creating a new ui button and assign to it the function to be called?

    • @mangosquirrel
      @mangosquirrel 4 ปีที่แล้ว

      @@gamedevel22 Yes, I followed the steps but the button is just unclickable, and hovering over it doesn't make it darker either

    • @gamedevel22
      @gamedevel22  4 ปีที่แล้ว

      Maybe some other ui object is over it and Blocks the touch

  • @kayPlayz.
    @kayPlayz. 2 ปีที่แล้ว

    good tutorial but music annoying

  • @beqab90
    @beqab90 ปีที่แล้ว

    it dosn't work don't ewaste your time.

  • @whateverplus8928
    @whateverplus8928 ปีที่แล้ว

    Horrible tutorial doesn't even cover how to add health, or take damage when a player is hit.