How To Create 2D HEART/HEALTH System (Unity Tutorial 2022)

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

КОМЕНТАРІ • 139

  • @SpeedTutor
    @SpeedTutor  3 роки тому +1

    Let me give you my heart and we'll learn this together!
    🔥UNITY SAVINGS: bit.ly/UnitySalesHub
    🔥SpeedTutor Unity Store: bit.ly/STUnityStorePuzzlePacks
    🔥HUMBLE SAVINGS: bit.ly/HumbleBundleDeals

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

    hi, for anyone that hasn't realized yet, if you change the " - bombDamage" or whatever other name you're using, for a " + bombDamage" you can create aswell a healing script if you want to put some extra lifes in your game, also thanks a lot for the tutorial :D

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

      Thanks for the suggestion! Thanks for checking out the tutorial.

  • @shiroyasha302
    @shiroyasha302 6 місяців тому +2

    BRO IM STRUGGLING FOR 2 DAYS AND YOU SAVED ME THANK YOU

    • @SpeedTutor
      @SpeedTutor  6 місяців тому

      I'm very glad you found this and I could help you! :) What have you been working on?

    • @shiroyasha302
      @shiroyasha302 6 місяців тому +1

      @@SpeedTutor I'm working on an adventure rpg game and this video helped me a lot

    • @SpeedTutor
      @SpeedTutor  6 місяців тому

      Amazing! How is your progress?

    • @shiroyasha302
      @shiroyasha302 6 місяців тому +1

      @@SpeedTutor It's going pretty well. Thank you for asking 🙂

    • @SpeedTutor
      @SpeedTutor  6 місяців тому

      Awesome, best of luck with it! :D

  • @miskasstories8035
    @miskasstories8035 7 місяців тому +2

    Helpp! I have done everything in this video as he said, but If i touch the bomb it just gets pushed by the player. I have checked the skript multiple times but everyting is correct. The health system works aswell, as i have testet it the same as he did. Please help!

  • @lill.johnysparkour
    @lill.johnysparkour 2 роки тому +5

    For those who have problems with taking damage not working, make sure to set the players tag to "Player"

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

    I was looking for this system all day long and ı cant resolve my game problem thank you so much again.Adamsın.

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

      I hope you found this useful! :)

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

    Very Helpful. Ran into some problems but in the end it worked out. Thanks!

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

      Awesome, how did you fix it?

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

    Dang.
    This is how I learned that you can't add Scene Objects to prefabs.
    Rather than using "bomb damage" for my game, I created enemies that would do damage and take away a heart from the player on collision.
    After following this tutorial, I found out that as long as I have an Enemy Object already placed in the game, it works.
    But if I have this Enemy as a Prefab, I can't add the "Health Controller" to the Damage Controller script that is attached to my prefab.
    Surely there is a way around this (it looks like you can do this through a script, but was stated that this is slow).
    Any other suggestions to have this work?
    I guess the point is that prefabs can only reference other prefabs and not Scene Objects because if you change Scenes, it wouldn't have anything to reference... so why not just create the Scene Object in each scene if you are using the prefab in each scene and allow to apply Scene Objects to prefabs???

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

      Hey there, I had the same problem like you where when I went to attach the 'Health Controller' game object to my Damage Controller script, it wouldn't let me and would instead say 'Type Mismatch' when I tried selecting it... You said you solved by creating enemies and taking away a heart from the player BUT HOW...? did you make an entirely new enemy??

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

      WOW thanks for the super quick reply and GOOD NEWS - IT WORKS, my game finally works, i just did what you said and BOOM it worked soo to you i owe a big thank you :D

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

      how did he solve it? the comment disappeared.

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

      @@mrlaz9011 ahh that happens somethings, dw thou i shall paste it here for you:
      Oh it's been a minute now and I have a lot of things that happen when my player takes damage, but I had to basically just reverse the way I thought about it. Rather than thinking "When this enemy touches the Player, cause damage to Player" I instead did "When Player touches the enemy, cause damage to the Player". It's the exact same thing, but before, I was trying to attach this script and a Game Object to a prefab and that just does not work. It doesn't allow that. But you can attach a Game Object to another Object (like the Player in my case). So don't try to attach a script and Game Object to a prefab, attach it to something else in your game. I also did some wonky stuff to make it work with other things, like making one script reference another script. I'm still super new at coding, so may want to ask someone else for help, especially cause as I go back over my own code, it's pretty messy. I have scripts like "PlayerHealth", ""DamageController", "Enemy", "EnemySpawner", "Health", that are all referencing each other and have a lot of different if statements to trigger certain things, that all probably could have been done in much more simple and elegant way.

  • @theterminator4793
    @theterminator4793 3 роки тому +1

    Hi!
    A couple of days ago I finished with your unity survival game series and I'm very happy with the results, thank you for your time and effort!
    Can you please make a video on a proper character death? Like when your thirst or hunger bar is at 0 you die of hunger/ thirst, would appreciate it

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      I really appreciate it! What do you want to learn? Just check whenever you update your health, when it reaches 0 you do whatever you want. Create a fail screen, play an animation, load a new scene? What do you want to happen?

    • @theterminator4793
      @theterminator4793 3 роки тому

      @@SpeedTutor Thank you for The reply!
      I was thinking of like a basic death screen, you know when the character dies, he drops down to the ground and then a text is displayed on the screen saying 'You died' and Then after a couple of seconds he respawns back to the starter point

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      I do have a respawning tutorial which might get you started on that! :) I'll note this onto my list, thanks for the suggestion.

    • @theterminator4793
      @theterminator4793 3 роки тому

      @@SpeedTutor I did check out that video, it was a cool video for a base of the death screen.
      Anyways thanks. I hope once that video Is out it would help others too.
      Thanks for replying!

  • @Shubham-bk2cf
    @Shubham-bk2cf 3 роки тому +1

    Can you recreate your survival horror series it is outdated now : ( if not full then only some specific videos?

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      What would you like to see?

  • @Kafyyyx
    @Kafyyyx 10 місяців тому +1

    THANKS YOU SOO MUCH but how do i keep the bomb, and not make it disappear?

    • @SpeedTutor
      @SpeedTutor  10 місяців тому +1

      No worries, thanks for watching! :) Just make sure you don't Destroy the object, or .SetActive(false) - Depending on how I did it in the tutorial.

    • @Kafyyyx
      @Kafyyyx 10 місяців тому +1

      @@SpeedTutor ye thank u so much I figure it by my own after some time!! Btw I just want you u to know that u saved my life because in 6 days I need to send my videogame to my teacher and I was stuck on the hearts for like 3 days

    • @SpeedTutor
      @SpeedTutor  10 місяців тому +1

      Awesome, good job!

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

    Thank you so much i really appreciate it i'm building a test game and this is just what i need

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      Glad I could help! What progress have you made?

  • @random_precision_software
    @random_precision_software 3 роки тому +1

    You using a new editor? Did you get this idea from my black and white problem? I've thought about doing it this way, but I wouldn't work the way I want it.

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      New Unity Editor? This is 2019.4 or 2020, I can't remember! Haha. Did you sort your colour issue?

    • @random_precision_software
      @random_precision_software 3 роки тому

      @@SpeedTutor no remember I was asking how to make the screen black and white, and you suggested using lut, in post processing? No I haven't sorted it yet.

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      Yes, I remember! I'm not sure what you meant by new editor though?

    • @random_precision_software
      @random_precision_software 3 роки тому

      @@SpeedTutor Oh yes I meant your intro looks more ZAPPY like your using a new VIDEO EDITOR ..sorry I should of explained better lol

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      Oh haha! I did create a new intro and I've been trying to improve the overall quality of my videos. The channel is really struggling so I'm trying to make everything as good as it possibly can! :)

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

    Hi do you have any tips on how to make the hearts change sprites to a half heart instead of just turning black?

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

      You could add more sprites to the array, but with them split in half? :)

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

    this tutorial helped me so much! thanks!

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

      I'm really glad it did, thanks for coming to watch! :)

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

    its not working. the heart is not turning black

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      Any errors?

    • @angeloblanco3115
      @angeloblanco3115 3 роки тому

      @@SpeedTutor no its ok now its working properly thanks awesome tutorial😍😍😍

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

    Hello, It Works For Me But I Wanna Ask, Can I Disappear Hearts Instead Of Turning Black. Can You Help Me ?
    edit: I Got It Working Thanks For The Tutorial Again !

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

      Awesome! What did you do?

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

      @@SpeedTutor I disabled the Image components of hearts via code instead of turning black. The code is Something like this: hearts[i].enabled = false;

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

      @@kutsall28 That's what I would have suggested! Awesome work around. :) You don't even need me!

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

      @@SpeedTutor Thank You Man !

  • @magicle855
    @magicle855 3 роки тому +1

    its not working and i dont have any errors aswell and i added a Debug.Log("Hit"); in the OnTriggerEnter2D and i dont even get the Debug

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      Did you follow my Trigger event checklist tutorial? Does your character have a Rigidbody2D component?

    • @lill.johnysparkour
      @lill.johnysparkour 2 роки тому

      Make sure to set the tag of your player character to " Player "

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

    thanks

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

      You're very welcome, my friend!

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

    thank you, it was very helpful!

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

      I'm glad you think so, thanks for coming along to check this out! :D

  • @ScreamPuffXD
    @ScreamPuffXD 3 роки тому

    I'm looking to do a system like this but with half hearts, how would I go about doing that?

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      Split the hearts into two sprites each, then add more to the array! :)

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

    omg¬¬¬¬thx ssssoooo much!!!!!!!!!!!!!!!!!!!!! u made this really clear and understandable

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

      You're very welcome, Ava! Good luck with your project. :)

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

    Question! How can you add this to like a shop system where if you pay a certain amount you can increase your heart by 1?

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

      You could probably use a list instead of an array, then you can add one to the list for what you buy in the shop and do it all in the same way! :)

  • @StigDesign
    @StigDesign 3 роки тому +1

    Great tutorial :D

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

    If you run into a problem where a player goes through an object that was meant to damage them but it doesn't, try this.
    In the damage controller, change the code to this.
    void OnTriggerEnter(Collider other)
    {
    _healthController.playerHealth = _healthController.playerHealth - badCoinDamage;
    _healthController.UpdateHealth();
    gameObject.SetActive(false);
    Destroy(gameObject); // Optional, destroy game object on collision
    }
    }
    change the variable names to your own.

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

    how do i save the hearts though scenes and also how do i reset hearts when they hit 0?

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

      You could use playerprefs for saving something simple. :)

  • @sz-ne7kh
    @sz-ne7kh 3 роки тому +1

    Great tutorial

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

    Hi i know this video is a year ago but what do i do when i want to set the health controller to a prefab thats in assets and not in the scene since its not letting me set it

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

      OHH I had this same problem and fixed it a little while ago by simply reversing the idea instead of having it so when 'enemy touches player, player takes damage' I did 'When Player touches enemy, player takes damage' as you can see its the EXACT same thing.
      In Unity create a new tag called 'Enemy' and the in your code where it says compare.tag("Player") change it to compare.tag("Enemy") and then attach this script to the players gameobject (your main character). hope this helped and isn't too late :)

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

      I just did that and it worked! thanks for posting this!

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

      @@mrlaz9011 BRILLIANT!! It's great to see it helped someone in the end

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

    Is it just me but does it just refuse to work for some people like it say i have the code completely correct but for the love of god it just won't work

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

      Any errors?

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

      @@SpeedTutor the code itself had no error, it seem to be more of the collision not registering, i tried it twice, first followed you exactly it didn’t work, I scraped it then tried again but this time I used the health system from a black thorn prod which didn’t change much aside from having an empty heart sprite, still didn’t work and now I’m just stumped

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

      @@SpeedTutor could you tell me how you had your 2d collider and rigid body 2d was set up please

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

      Just one collider2d and ridigbody2d on my player and the same on my other items! :)

    • @lill.johnysparkour
      @lill.johnysparkour 2 роки тому

      Try setting the player tag to "Player"

  • @cine-de-daniboy
    @cine-de-daniboy Рік тому

    Hello, I just have one problem when I write playerhealth or Updatehealth in the DamageControll script it does not recognize it. Do you have an idea of why this is a problem? Good tutorial btw.

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

      That's very strange, is the method public?

    • @cine-de-daniboy
      @cine-de-daniboy Рік тому

      @@SpeedTutor I solved it, I was just not telling my script where it should be getting the playerhealth, Im sorry I was tired, but thank you

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

    amazing stuff

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

      Thanks very much for watching! :)

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

    how to regain a heart if we collide with a heart sprite in game

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

      Just do the opposite to bombDamage, do += bombDamage or create a new variable called "healthToHealth" :)

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

    did everything you did i got the scripts working but it does nothing when a object collides wiith my player

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

      Did you tag the player?

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

      @@SpeedTutor I did, I’ve tried adding the code to the player movement aswell but still the same result

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

      Same for me it does not work and I checked EVERYTHING

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

    I can’t get the damage to work I have everything copied exactly and the object doesn’t deal the damage or disappear

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

      Did you attach the correct components?

    • @lill.johnysparkour
      @lill.johnysparkour 2 роки тому

      Set the players tag to " Player "

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

      @@SpeedTutor I got it to work I made 2 box colliders and put is trigger on one

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

      @@dxrku7290 Thanks so much!! I just had the same problem and you're such a life saver

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

      @@dxrku7290 two box colliders on enemy or one on player and one on enemy?

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

    It's not working for me. I figured out that the Box Collider2D has to be set to Is Trigger, but then my enemy will just fall through the floor, which is a problem because It's not a static enemy. The other issue, when I lock my enemy on all axis(as is your bomb) and set it to Is Trigger, is that the health does not update when I trigger with the collider of the enemy the enemy is destroyed though.

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

      You could add a parent collider to the object as well?

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

      @@SpeedTutor what do you mean a parent collider. I have the same problem. Everything works fine. It's just I don't want a static enemy. I want to add this script to enemies that have an AI path and movement.

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

      @@jeremystonecipher4159 If you have two box colliders to your AI, scale one of them down a bitt and untick "Is Trigger" On the bigger Box collider check "Is Trigger. This should work with a moving AI with rigid bodies. Let me know if it worked :)

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

      ​@@lill.johnysparkour It did! thank you.

  • @AIWarper
    @AIWarper 3 роки тому

    It doesn't let me drag the Healthcontroller GameObject into the script once I add it to my Prefab (which is just a 3d sphere with a rigidbody and sphere collider) Any idea why?

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      Any errors messages in the console?

    • @AIWarper
      @AIWarper 3 роки тому

      @@SpeedTutor Nope. When I click and drag over the controller it just doesn’t let me drop it into the slot

    • @AIWarper
      @AIWarper 3 роки тому

      @@SpeedTutor any thoughts? If not I’ll post it to the unity forums to see if they can help me. 👍🏻

    • @AIWarper
      @AIWarper 3 роки тому

      It's worth noting if I drop the prefab sphere into the scene hierarchy I can apply the health controller just fine. But as soon as I try to save it as a prefab it removes itself

    • @Mysticscubing
      @Mysticscubing 3 роки тому

      You Have To Make The Script A Prefab

  • @_Garm_
    @_Garm_ 3 роки тому +1

    yes please! :D

  • @a.technology1446
    @a.technology1446 3 роки тому +1

    God bless you 🙌❤️

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

    it keeps saying for me that the name "_healthController" doesnt exist i have no idea why

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

      Is it because your variable has a different name?

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

      @@SpeedTutor figured it out now it wouldnt work at first so i edited the script a little bit just to realize I didnt have "is trigger" ticked on my enemies haha

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

      Haha, good work! Usually it's a tiny thing that can cause the most annoying issues.

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

      @@SpeedTutor I know right thanks for the tutorial anyway man

  • @luizhenrique78945
    @luizhenrique78945 3 роки тому +1

    Its not working. the Player is not taking damage.

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      That's good, isn't it?

    • @luizhenrique78945
      @luizhenrique78945 3 роки тому

      @@SpeedTutor I wrote wrong

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      Did you mean something else?

    • @magicle855
      @magicle855 3 роки тому +1

      it dosent work for me aswell the player dosent take damage when he hit the bomb

  • @jag-ca
    @jag-ca 2 роки тому

    Does this still work? if so. WHY AM I GETTING SO MANY ERRORS!

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

      It does work, what errors are you having?

    • @jag-ca
      @jag-ca 2 роки тому

      @@SpeedTutor enough to have had forced a factory reset on my pc

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

      @@jag-ca dang

  • @siggetiger5239
    @siggetiger5239 3 роки тому

    its not working my heart is not going black

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      When you take damage? Any errors?

    • @siggetiger5239
      @siggetiger5239 3 роки тому

      ​@@SpeedTutor I have not gotten there yet. its not working after the first script (google translate. im swedish)

    • @SpeedTutor
      @SpeedTutor  3 роки тому

      Let me know, good luck! :)

  • @justsync5879
    @justsync5879 3 роки тому +1

    its not working

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

    how do i fix
    IndexOutOfRangeException: Index was outside the bounds of the array.
    HealthController.UpdateHealth () (at Assets/HealthController.cs:35)
    HealthController.Start () (at Assets/HealthController.cs:15)
    my code is this pls tell me what the problem is
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    public class HealthController : MonoBehaviour
    {
    public int playerHealth;
    [SerializeField] private Image[] hearts;
    private void Start()
    {
    UpdateHealth();
    }
    public void UpdateHealth()
    {
    if (playerHealth