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
0
Question by Beta4 · Jul 15, 2019 at 08:42 PM · floatssubtracting

Sometime when I subract a value to hit zero I get a -1

So this might be for best practices too but I have a value that is a float value. I have the float value subtracting going backwards until it hits zero but maybe 1 out of 20 times it will give me a value of -1. What call or function would be best to use to avoid this problem from ever occuring. Once it hits 0 to stop subtracting then I have another check that if floatValue =less than 0 to then floatValue = 0. (I'm asking this question on my phone since I'm away from my computer at the moment and there is apparently no less than sign on my keyboard lol.) I'd like to know as this has me puzzle I'm still trying to learn about coding and maybe someone could help me with general best practices in this case to be a better programmer. Thanks again.

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 metalted · Jul 15, 2019 at 09:03 PM

When you are subtracting floats using for instance Time.deltaTime, it will never land on 0, it will always overshoot. So in your code you check if its smaller than 0 instead of checking if its equal, because that will not work. Sometimes it might overshoot so much that it will be rounded to -1 instead of 0. There isn't a single way to correct this. The only thing that is sure, is that you have to handle it on the moment it happens. So when your if statement checks if it is smaller than 0 and its true, just set the value to 0:

  if(timerValue < 0)
     {
         timerValue = 0;
         //Code...
     }

If it is a timer you are using, you could also have a float timer and an int timer. The float timer will be the actual counter, and the int timer will be a rounded version of the float timer. That way it will always be exactly 0 and the problem is gone: float timer = 10; int roundedTimer;

 public void Update()
 {
     timer -= 1 * Time.deltaTime;
     roundedTimer = Mathf.FloorToInt(timer);
     
     if(roundedTimer == 0)
     {
         //Do something...
     }
 }

A side effect of this though, is that the rounded value (depending on if you Ceil, Floor or Round) will never be the same as the actual value and will be wrong by 0.5 seconds max. Maybe if you don't have an if statement but you still don't want it to overshoot, you could use something like this:

 float timer = 10;
 
     public void Update()
     {
         timer -= 1 * Time.deltaTime;
         timer = Mathf.Max(timer, 0);
         
         //Do something
     }

So as you can see, there are many ways to handle the situation. There is no way you must do it. Just make sure you handle it, before you use the data.

Comment
Add comment · Show 1 · 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 Beta4 · Jul 16, 2019 at 01:26 AM 0
Share

Thank you for helping out , I have tried the last suggestion, I will post back in a few days as I continue to test and see if that fixes the problem as I suspect it will.

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

109 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

Related Questions

Alternative to Update 1 Answer

float changes sign randomly 0 Answers

How can I make this only act once? 3 Answers

Character moves off to side and floats 0 Answers

Have slider subtract from one variable to add to itself. 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