Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by benenorthrop · May 10 at 11:07 PM · programminghealth barhealth-deductionhealth

I need some help with a Fractional Heart System?

I'm trying to create a heart system that displays my health with 5 hearts and reduces in fractions every time. I want to make the maximum health 200 so that means every quarter is 10 and every heart is 40.

I know that you have to reference 5 different heart sprites by using 5 cases and assigning each one then when generating them you just use a prefab to spawn them in a horizontal layout group, but that's as far as my knowledge goes, and it's more I know how it works but not how to code it.

Thank you in advance for any help I get:)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · May 10 at 11:52 PM

You mean a system like this? Over there I made a WebGL example.

Comment
Add comment · Show 2 · 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 benenorthrop · May 11 at 12:10 AM 0
Share

That is sort of the system I want, But it's More Where I have 5 hearts throughout the whole game and each heart stores 40 health and 10 for every section of the heart. I've made 5 sprites for the game with different amounts of the heart missing: Full, Three-quarters, Half, Quarter, Empty. I followed a tutorial before on how to make a system, but it adds more hearts every time and calculates the hearts in an annoying way and my brain isn't big enough to program a new version of it. I understood the part where he used a switch statement and enums to switch the heart if it goes into a value from 0 to 4, but I don't understand how to program the drawing of the hearts and updating it when hurt. I know the basic principle, but I'm not able to program it on my own.

[1]: /storage/temp/196173-healthexample.jpg

healthexample.jpg (282.3 kB)
avatar image Eno-Khaon benenorthrop · May 11 at 02:42 AM 1
Share

It should all work in fundamentally the same manner, but you would just apply one more multiplication/division to account for your fraction-of-a-fraction heart values.

 int healthPerPartialHeart = 10;
 int partsPerHeart = 4;
 
 // ...
 
 int fullHeartCount = hp / (healthPerPartialHeart * partsPerHeart);
 int spriteIndex = (hp / healthPerPartialHeart) % partsPerHeart;
 
 // Example: 200 hp
 // fullHeartCount(5) = hp(200) / (10 * 4) = 200 / 40 = 5
 // irrelevant sprite index at full health
 // spriteIndex(0) = (hp(200) / 10) % 4 = 20 % 4 = 0
 
 // Example: 134 hp
 // fullHeartCount(3) = hp(134) / (10 * 4) = 134 / 40 = 3
 // spriteIndex(1) = (hp(134) / 10) % 4 = 13 % 4 = 1

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

158 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 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

Multiple Cars not working 1 Answer

How to make my health decrease? 1 Answer

How to Make Health Decrease Over Time 2 Answers

Player health dropping far to fast 1 Answer

How do I create a health system composed of multiple images? 0 Answers


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