Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by sklorticus · Jan 16, 2018 at 05:14 PM · 2d-platformernoobhealthbar

Health Bar/Hearts?

So, I've been researching for WEEKS on end how to make a working heart system for a 2D Unity platformer I'm doing. Only problem is, I'm a total noob just about all things Unity here, and I'm a wee bit new to coding (though I understand C# and how it works). The biggest problem I've been having is using a health system where when the player is hit, a heart vanishes. While I'm absolutely aware that there's already a thread on the topic, most of it suggests that I use GUI, which I absolutely hate, and the code that doesn't suggest I use GUI settings is a bit too complicated for the timeframe I have, and I run the risk of ruining my game. I know I'm asking a lot, and I don't expect anyone to just WRITE THE CODE for me, I just need a bit of a for-dummies rundown on how this all works.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sklorticus · Jan 16, 2018 at 05:18 PM 0
Share

Also for clarification, I'll note that the health code is in a separate script from the player controller code, if that happens to be relevant.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by OrionGamesInc · Jan 16, 2018 at 05:22 PM

This is definitely not perfect, but it may be useful to learn how the sprite fill system works in Unity.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Player : MonoBehaviour {

     [Header("Player")]
     public Player player; //the player object that holds the player script

     [Header("Stats")]
     public int maxHealth; //total amount of hearts
     public int health; //current number of hearts
 
     [Header("UI")]
     public Sprite healthUI; //use an image of four hearts next to eachother, spacing will help make it look better
 
     void Update(){ //change the value of your sprite using a horizontal fill, space the hearts out so that 1 includes only 1 heart and so on
         maxHealth = player.maxHealth;
         health = player.health;
         
         healthUI.fillAmount = health / maxHealth; //will always return a decimal for percentage
     }
 }

EDIT: I just read your comment and you can include a reference to the Health object if the Player script is attached to your player, just drag it into the slot in the inspector.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sklorticus · Jan 16, 2018 at 05:25 PM 1
Share

Thank you so much! I'll test this out right away.

avatar image sklorticus · Jan 16, 2018 at 05:28 PM 0
Share

Although, quick noob comment here: When you say it's in the Player class, you mean I should put it in the character controller script, right? (Sorry if that's redundant of me to ask, just wanna make sure I don't wreck my code by putting it in the wrong place)

avatar image OrionGamesInc sklorticus · Jan 16, 2018 at 05:29 PM 0
Share

updated the answer, hopefully that helps. if not i'll try to help out more

avatar image sklorticus OrionGamesInc · Jan 16, 2018 at 05:32 PM 0
Share

thank you for being patient w/ me! it makes more sense to me now.

avatar image
0

Answer by Constantin_d · Jun 10, 2020 at 01:37 PM

tutorial about how to make a heart bar system only using sprites and a small amount of code:

https://www.youtube.com/watch?v=Dg-6qoZ2JqI&list=PLyz_wKnxtoQO0Tf3koreC4gJtFv4213yQ

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

i cant tell if unity is glitching for me 0 Answers

OnGUI() not showing anything 2 Answers

Does anyone know why those blue lines appear when the camera and background moves? 1 Answer

Help making basic health bar script 1 Answer

Noob Question -- Programming 2d platformer 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges