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

Поделиться
HTML-код
  • Опубликовано: 16 ноя 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 Год назад +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  Год назад

      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

  • @michael.corlenome
    @michael.corlenome 2 года назад +5

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

  • @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?

  • @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! :)

  • @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?

  • @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! :)

  • @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!

  • @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!

  • @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

  • @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? :)

  • @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. :)

  • @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! :)

  • @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?

  • @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.

  • @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.

  • @sz-ne7kh
    @sz-ne7kh 3 года назад +1

    Great tutorial

  • @StigDesign
    @StigDesign 3 года назад +1

    Great tutorial :D

  • @magicle855
    @magicle855 2 года назад +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  2 года назад

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

    • @michael.corlenome
      @michael.corlenome 2 года назад

      Make sure to set the tag of your player character 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

  • @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 !

  • @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! :)

  • @cazterk
    @cazterk 2 года назад +1

    amazing stuff

    • @SpeedTutor
      @SpeedTutor  2 года назад

      Thanks very much for watching! :)

  • @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! :)

  • @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. :)

  • @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

  • @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

  • @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! :)

    • @michael.corlenome
      @michael.corlenome 2 года назад

      Try setting the player tag to "Player"

  • @_Garm_
    @_Garm_ 3 года назад +1

    yes please! :D

  • @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

  • @a.technology1446
    @a.technology1446 3 года назад +1

    God bless you 🙌❤️

  • @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" :)

  • @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?

    • @michael.corlenome
      @michael.corlenome 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?

  • @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😍😍😍

  • @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.

    • @michael.corlenome
      @michael.corlenome 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

      ​@@michael.corlenome It did! thank you.

  • @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

  • @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 2 года назад +1

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

  • @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