• 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 Abysinian · Jan 30, 2015 at 12:18 PM · unity 2dsliderhealthbarhealth barhp

Slider value going to 3.911555e-07

Hi there,

I'm using a Slider for my HP and MP bars in a game I'm developing, and I'm using this code:

         if (other.gameObject.name == "Enemy" && healthBarSlider.value > 0)
         {
             healthBarSlider.value -= 0.025f;
             hpText.text = "HP: " + System.Math.Round(healthBarSlider.value * 100,0);
             DamageTaken();
         }
         else
         {
             gm.ReloadLevel();
         }

This code makes my player lose 5HP per hit from their max of 100. When it reaches 0 it actually shows as 3.911555e-07 in the inspector, and the level doesn't reload. If I take another hit, then it reloads. Does anyone have any idea why this might be happening or how I can fix it? I've tried setting the damage value to 0.1f and the problem doesn't occur then, it's very strange.

I've also come across another problem, which is that the DamageTaken() function never gets called, despite the code above/below it being run.

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
1

Answer by Scribe · Jan 30, 2015 at 12:44 PM

It is caused by floating point error, you could solve it by saving your health and any damage does as integer values e.g.

 int maxHealth = 100;
 int health = 100;
 int dmg = 5;
 
 if (other.gameObject.name == "Enemy" && health > 0){
     health -= dmg;
     healthBarSlider.value = (float)health/maxHealth;
     hpText.text = "HP: " + health;
 }else{
     gm.ReloadLevel();
 }

That is untested but hopefully you get the idea, the problem is that there is no way to represent every decimal value as the real numbers are a continuum, so for every two chosen numbers a, b, a =/= b, there still exists and infinite number of values between them, no matter how arbitrarily close a and b are. As computers only have a finite amount of memory, real valued calculations cannot all be done perfectly, unless you have some nice starting values 0.5 for example can be represented exactly with bytes with the standard representation of a float

Scribe

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 GameVortex · Jan 30, 2015 at 12:53 PM 1
Share

Floating point inaccuracy.

avatar image Bonfire-Boy · Jan 30, 2015 at 01:16 PM 0
Share

Or if all that matters is how it's displayed in the hpText object, you could just use $$anonymous$$ath.RoundToInt() ins$$anonymous$$d of plain Round() when setting the text value.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make slider function properly after an animation? 2 Answers

Healthpickup isn't working 1 Answer

Health Bar 0 Answers

Needle injection slider 0 Answers

Dynamic slider size with the new UI 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges