• 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 t$$anonymous$$s 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 $$anonymous$$ts 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 t$$anonymous$$s problem from ever occuring. Once it $$anonymous$$ts 0 to stop subtracting then I have another check that if floatValue =less than 0 to then floatValue = 0. (I'm asking t$$anonymous$$s 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 t$$anonymous$$s has me puzzle I'm still trying to learn about coding and maybe someone could help me with general best practices in t$$anonymous$$s 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 t$$anonymous$$s. The only t$$anonymous$$ng 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 somet$$anonymous$$ng...
     }
 }

A side effect of t$$anonymous$$s 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 somet$$anonymous$$ng like t$$anonymous$$s:

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

how can i substract one axis by 180? 2 Answers

How can I create a GUI with a variable from another script? C# 1 Answer

I can't get variable to decrease by one 1 Answer

Breaking up a string every A,B,C,D and store value behind it 3 Answers

How do I subtract a variable multiple times. 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