Player Health System #2: Heart Display UI (Unity Tutorial)

Поділитися
Вставка
  • Опубліковано 22 лис 2024

КОМЕНТАРІ • 85

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

    Thank you so much! This has helped me very much. I've been trying to make a health system multiple times, but i failed miserably. Great tutorial!❤

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

    Amazing tutorial! Just did it and works perfectly

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

      Awesome! I love hearing that. Good luck with the rest of your project!

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

    Thank you so much for this great tutorial! I wouldn't have figured this out alone lol

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

      So glad to hear it helped! Thanks for sharing.

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

    these tuts are amazing

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

    Cool I really needed this

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

    yo thanks so much this really helped me out!!

  • @andreasquerat4006
    @andreasquerat4006 7 місяців тому +1

    Great Vid ! Just wondering if my player change scene is this still functionnal ?

    • @NightRunStudio
      @NightRunStudio  7 місяців тому

      It will work, but as with any scene change stuff, you have to decide how you want it to persist. For example, I would use DontDestroyOnLoad on both the UI and the player so that they persist when you scene change.

    • @andreasquerat4006
      @andreasquerat4006 7 місяців тому +1

      @@NightRunStudio Oh yes thank you very much ! I click subscribe your so fast to response it's amazing (and sorry english is not my first language)

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

    I got the whole script and everything to work, but I was wondering if it was possible to make it go to full hearts, THEN HALF hearts, before finally going to empty hearts. I already changed the heart value based on another comment, I just don't know how to change the sprites correctly between the three. (Great tutorial btw)

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

      Thanks! I'm glad your finding the series helpful.
      The way the code is currently written, it wont naturally work for half sprites (since you would need to enable different sprites depending on whether it is a half or whole heart). That said, you could alter the code, and instead of changing the sprite from an empty to a full one based on your health, you could make all of your sprites half hearts, put them into your heart array, and then just enable or disable them based on health.
      In that case, your code would look something like:
      for(int i = 0; i < hearts.length; i++)
      { if ( i < health)
      {hearts[i].sprite.enabled = true;}
      else
      {hearts[i].sprite.enabled = false;}
      }
      That way you could load the left side of the heart sprite into slot 1 on the array, and the right side in slot 2 and it would enable/disable based on your health.
      I hope tha helps!

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

      @@NightRunStudio I got it to work. Thank you very much!

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

      @@abaier82 Awesome! Congrats on figuring out how to adapt it for your project. I always find that the most rewarding!

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

    How do I make it so that the player would only lose a heart if it receives a certain amount of damage? Like if I have 100 player health and if I take 20 damage I would lost a heart, but not if I only took 15. Thanks for the tutorial btw.

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

      I think the easiest way to do this would be to decide how much health each heart is worth in your game, then multiply you "i" (integer) by that number. So, if each heart is worth 15 health...instead of:
      if(i < health)
      you could write:
      if(i * 15 < health)
      though I would recommend create a variable so that you could change this value as your player levels. I would call this "heartValue" or something like that. Then you could write:
      if(i * heartValue < health)
      I hope that helps!

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

      @@NightRunStudio thanks so much!

  • @MichaeladeXD
    @MichaeladeXD 2 роки тому +4

    woah coding class

  • @cheery-hex
    @cheery-hex 2 роки тому +2

    okay I cannot for the life of me drag anything at all into those heart slots (the amount). nothing shows up under select images either. triple checked steps carefully :(

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

      Are your empty and full heart slots working? Or are all three not letting you drag stuff in? If all of them are the problem, did you remember to type "using UnityEngine.UI;" at the top of your script? If it's just the array that is the problem, did you make sure to add your hearts to the hierarchy as UI --> Image? (If they are normal sprites, they wont work). Hope one of those helps!

    • @cheery-hex
      @cheery-hex 2 роки тому +1

      @@NightRunStudio thanks much NRS. I have all those yes but I will keep searching it will work eventaully. thanks!

  • @Fwsr.
    @Fwsr. 2 роки тому +2

    i cant find any goot sprites for it do you have a link to the download of these sprites?
    Great vid btw deserves way more attention

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

      Nvm found one. I got a script where if i die i get respawned via prefabs. But if i do all the hearts in the [ ] array are gone. Do you know how i could fix that?

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

      ​@@Fwsr. Glad to hear that you found some graphics assets!
      As for the problem you describe, this is definitely happening because your prefab doesn't know what heart images it is supposed to interact with. I can think of two main ways to fix this:
      1) Don't use a prefab for your character. Instead of spawning a new character, just teleport the existing one back to a spawn point and set its health back to full. (This would be the easiest fix).
      2) Since you can't drag your heart images into your prefab's script, you will need to code it. You can do this in your Start() function (so that it happens as soon as your character Instantiates into the scene). It would look something like:
      Hearts[1] = GameObject.Find(Heart1).GetComponent();
      Hearts[1] refers to the array, so you'll need to do this for all of the hearts on your player.
      (Heart1) refers to the name of the game object (in your hierarchy) that you are trying to reference.
      I hope this helps! Good luck with your project.

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

    for some reason when i add the image for some reason the canvas gets really big and i cant do anything about it what should i do?
    love your tutorials btw

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

      Hmmn.... I'm not totally sure what you mean. The Canvas should always be the same size (it's not editable, but is always ginormous). I always like to put my scene and game view side-by-side, that way I can move the images around in the scene view, and see what it will look like in the game in my game view. Not sure if that addresses the problem, or if it's something else.

  • @SorceressGamelab
    @SorceressGamelab 20 днів тому +1

    Hi! I'm using your heart system and i was wandering how can i make it so when player picks up an item he will get one additional heart? Do i have to add to max health or do i need to do an additional step? Thank you!

    • @NightRunStudio
      @NightRunStudio  19 днів тому

      The system isn't inherently designed to do this, but you definitely could add that functionality. What you would want to do is:
      1) Add all of the hearts that a player could possibly have right from the start.
      2) In your Start() method, create a for loop that iterates through all of the empty hearts, turning them on as long as their length is not greater than your maxHealth.
      3) The rest of your code would work as normal.
      4) Now, whenever you gain maxHP you would want to call the same method you did in Start() so that it can turn on new empty hearts.
      It's hard to get it across without another tutorial, but hopefully that gets you off on the right foot.

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

    Useful video man just learn utube algorithm to reach more people

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

      Thanks for that. I obviously have some research to do :)

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

    thank you for the tutorial! i was wondering is there a way for a heart animation to be played when the character is hit?i already have the animation i just dont know when to play it.

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

      You can definitely do this, though it will help if you are pretty comfortable with Unity's animator already.
      1) In your animator, create a LoseHeart animation (or whatever you want to call it), but make sure that it has a bool condition (maybe call it "lostAHeart"). Make sure that the only transition out of this animation has a condition that includes lostAHeart being false.
      2) In your healthDisplay script, make sure that you have a reference to your animator up at the top:
      public Animator heartAnim;
      3) Now, right above the emptyHeart line in your for loop, set your animator bool:
      heartAnim.SetBool("lostAHeart", true);
      4) I think you can get rid of the emptyHeart line altogether. As long as your animation ends with an emptysprite and it is NOT on loop, it should just keep playing the empty heart sprite until your health gets restored.
      5) Don't forget to hook up your animator in the Inspector in Unity.
      That should get the heart animation playing, and your heart should stay empty until your health is restored. You will probably want to do the opposite when gaining health (create a health gain animation that plays if lostAHeart is false.
      If you run into trouble implementing, feel free to pop into my discord server and post some code. Either myself or one of the members can probably help out. Cheers! discord.gg/G3eQqaCYq

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

    Got a bit confused when your Health Display script suddenly changed to Heart Display script at 8:40 and the order of the Hearts list was different. (beneath maxhealth) after that, both health numbers dropped with damage but the hearts stayed the same.

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

      So sorry about that! This was one of my earliest tutorials, and I sometimes missed stuff like that (I had to change the name due to a conflict in my own scripts, but should have noted it in the video so as not to confuse people. Thanks for pointing this out.

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

      @@NightRunStudio I thought that might be the case. It's a good clear tutorial and I did get it working. I had multiple Players that collectively contributed to the health bar so it took a bit of working out, but it all works great.
      I'm from a modelling/animation background. Full game coding is a new venture for me (I normally bring code devs in but I wanted to understand the principals) This has become my go to channel and I'm really enjoying it.

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

      @@DextraVisual That's awesome to hear! I love when animators and musicians and other creatives take the time to understand the code; I definitely think it broadens your overall understanding of the whole development process.
      Thanks for sharing, and good luck with your project!

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

    Is there a way for me to reference a separate game object instead of a sprite renderer? I have a game object that has a completely functional restart button, but I want it to be disabled and only enable when Health reaches 0, as opposed to the sprites that only get disabled when Health reaches 0. I know that instead of “.enabled = false” I would just use “.enabled = true” but I don’t know how to actually select the game object I want nor how to select any game object at all. If this is worded weirdly let me know and I’ll try to word it better, but even if you don’t see this thank you so much for these videos, I seriously can’t find anything that even comes close to the simplicity yet effectiveness of these that doesn’t require any prior knowledge in programming 😄😄

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

      You can reference a game object the same way you do with the sprite renderer. To turn it on or off though you have to use SetActive(true) instead of enabled. Enabled is for components, while setActive works for game objects.
      Once you make the reference to the GameObject at the top of your script, you can either drag your object into the new box in the inspector, or you can find it in your script by placing
      GameObject.Find(“your object name”)
      In your start function.
      Hope that helps!

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

    thank you so much

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

    Thanks!

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

    this is really helpfull, btw can i request a tutorial?, respawn tutorial pls

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

      Thanks for the feedback. Respawn would be a great tut. I’ll look into making one.

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

      @@NightRunStudio i will wait for it

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

    I'm using this in a 3D game, the mesh renderers would still show the last heart when the player dies, so I changed it to disable the player's game object and it is still showing the last 2 hearts when the player dies. Any solutions for this?

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

      It sounds like you may be disabling the player before your script runs the update on your heart UI. Did you try the suggestion at the very end (around 10:30) for disabling sprite renderer and movement instead of disabling the player completely? That way your health script can finish doing it's thing, but it will look like you died.

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

    Hey man great video, however my player does not have a sprite renderer instead i just use a GameObject & Mesh renderer for him. Is it possible for me to do the same thing you did in the video without a sprie renderer? I could really use your help

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

      I so wish I could help, but I’ve only ever worked with 2D, so I’m really not sure how this would map onto that. The basic structure would work for sure, but I’m not sure what you would want to do instead of changing sprites. Sorry about that!

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

      @@NightRunStudio nevermind i fixed it by instead of writing Spriterenderer i wrote Meshrenderer and also Trailrenderer since my player has a trail following him. Thanks for the tutorial man. Keep up the good work

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

    Bro you should make a boss system in unity with powers to the boss like spawning enemies while fighting teleportation n if the boss dies the boss will blast etc. these are my suggestions overall big thanks

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

      Thanks! I am hoping to do an Enemy AI series soon which will hopefully include a boss fight--so great ideas! Thanks for sharing.

  • @rabbit816
    @rabbit816 9 місяців тому

    Hello, I am unable to drag and drop the hearts into the list, do you have any ideas as to why?

    • @NightRunStudio
      @NightRunStudio  9 місяців тому +1

      Are the hearts UI images, or did your drag them in as sprites? Sometimes people just put them in as sprites, and then the array wont recognize them.

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

    Bro can you give the both hearts link? pls

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

      Unfortunately, those are just images I created for my own stuff. That said, there is a lot of great free art available out there. For example, OpenGameArt has some great stuff. Here's one link if you're interested, but if you don't love this one there's lots more out the to find: opengameart.org/content/simple-small-pixel-hearts

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

    I can't seem to find the empty heart sprite, can you give me the link to it? Thanks

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

      I'm afraid I don't have a Github or place to download resources at this point--hopefully as the channel grows I can add this. That said, openGameArt has some great stuff that might be helpful:
      opengameart.org/art-search?keys=heart

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

    Love this tutorial however, I have a problem with my sprite right at the very end where the script doesn't disable the sprite but it does disable the movement. Where have I gone wrong do you think?

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

      Sounds like there may be a problem with your SpriteRenderer reference. Are you getting any errors in your console?
      If there are no errors, you might need to use a debug line to try to find what is causing the problem. If you aren't familiar with doing this,
      you could try adding something like:
      Debug.Log(playerSr.gameObject.name);
      If you put that line right before the playerSr.enabled line it should print the name of the object that has the sprite renderer in your console. If it is your player's name, then you know your sprite renderer reference is good, if it's something else, then you know this is you problem.
      Hopefully that helps. Let me know if you have any other questions.

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

      @@NightRunStudio I have not got any errors when it plays.
      Cool, I will give that a go. I do have in Sprite renderer a box at the top that says Sprite and in the box "None (Sprite)" is that something that could be causing the problem?

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

      @@hanpolo2727 That seems weird that your sprite renderer box is empty--if it's empty, you shouldn't be able to see any sprite at all. Is it possible that you have multiple sprite renderers on this object (maybe in the object's children)? If so, it could be that you are disabling the sprite renderer that has no sprite, but allowing another one to continue to shows its sprite.

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

      @@NightRunStudio Oh wait a moment I may have it. My gaming teacher taught us when making the player, to have all scripts and RigidBody2D in a parent then animator and capsule collider 2D and then sprite renderer in a child. I also have a second child called Ground Check too.

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

      @@hanpolo2727 That could definitely be the problem--and that's good advice from your teacher! If you put sprites and physics on the same object, all kinds of weird stuff can happen.

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

    How is it possible that he only looses 1 heart instead of 2?

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

    Thank you so much it worked😭😭😭😭😭 making my dreams come true 🥲

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

      Hahaha! So glad it worked.

    • @shonmacklin9613
      @shonmacklin9613 5 місяців тому

      I felt this 😢😂 I will be trying this tutorial soon