Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 gregroberts · Dec 10, 2014 at 01:43 AM · floatintegerroundrounding-errors

How can I detect when a float value crosses an integer threshold?

I have a float value that I'm trying to have trigger a sound event whenever it crosses past an integer threshold. The user is able to increment the value in 0.1 increments.

In other words, whenever the value equals the nearest integer, I want to play a sound... so when the user takes it from 3.1 to 3.0, BING. and when they take it from -1.9 to -2.0, BING! and when they take it from 0.9 to 1.0, BOP!, and when they take it from -4.1 to -4.0, BOP! It seems that somehow my internal math manipulations are also slipping float values from time to time, transforming 3.1 into 3.0999999, so a solution adjusting for that strange variance would also be greatly appreciated.

Here's the code that works 30% of the time:

 var g_MC = new Vector4[n];

     function chargeDown(h) {
         g_MC[h].w -= 0.1;

         if (Mathf.Abs(g_MC[h].w) - Mathf.Round(Mathf.Abs(g_MC[h].w)) < 0.01) {
             AudioSource.PlayClipAtPoint(g_sFXchargeDown, Vector3(g_MC[h].x, g_MC[h].y, g_MC[h].z));
             Debug.Log("CHARGE DOWN ---- BING!");
         }
     }

     function chargeUp(h) {
         g_MC[h].w += 0.1;

         if (Mathf.Abs(g_MC[h].w) - Mathf.Round(Mathf.Abs(g_MC[h].w)) < 0.01) {
             AudioSource.PlayClipAtPoint(g_sFXchargeUp, Vector3(g_MC[h].x, g_MC[h].y, g_MC[h].z));
             Debug.Log("CHARGE UP ---- BOP!");
         }
     }

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

2 Replies

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

Answer by Owen-Reynolds · Dec 10, 2014 at 06:16 AM

Standard trick is to use a "trailer." In your case, just always remember the previous int value. Whenever it changes, fire and update.

As cdrann points out, negative numbers round funny, so add 1000 (whatever the lowest negative value is. Can also check for negative and round differently, but "make it positive" is easier):

 float oldIntVal = (int)(val+1000);
 ...
 float newIntVal= (int)(val+1000);
 if(newIntVal!=oldIntVal) { playSound(); oldIntVal=newIntVal; }
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 gregroberts · Dec 10, 2014 at 03:06 PM 0
Share

very elegant. and lightweight on the processor. thank you Owen.

avatar image
0

Answer by cdrandin · Dec 10, 2014 at 02:17 AM

One way to determine int threshold you are talking about would be to use modulo as follows:

1.0%1.0 = 0

1.1%1.0 = 0.1

...

1.n%1.0 = 0.n

2.0%1.0 = 0

For negative take into account and make sure to make it positive. Modulo works in the positive range. I believe this modulo works in C#

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

27 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

Related Questions

Why are floats not accessing my inputted values? 1 Answer

C# round transform.position floats to .5 2 Answers

Float to int casting in C# script with modulo giving division by zero error 1 Answer

Shown decimal places 2 Answers

Changing the thing to take decimal values.... 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