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 /
  • Help Room /
avatar image
1
Question by matthebest · Sep 17, 2015 at 12:09 AM · c#mathf

mathf.round problem

Trying to make a game so that when I click, its adds 1 but when I pass 10, there are 6 decimals after the number instead of one. Need help!

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class Main : MonoBehaviour {

 public float clicks = 0;
 public float autoClicks = 0;
 public int finger = 10;
 public GameObject clicker;





 void Update () 
 {
     Text text = clicker.GetComponent<Text> ();
     text.text = "" + clicks;

     clicks = mathf.round(clicks * 10f) / 10f;

     clicks += autoClicks * Time.deltaTime;

     if (Input.GetButtonDown ("Press")) {
         clicks += 1;
     }
     if (Input.GetButtonDown ("Press")) {    
         if (clicks > finger) {
             clicks -= finger;
             finger += 10;
             autoClicks += 1;
         }
     }
 }

}

Comment
Add comment · Show 2
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 kingcoyote · Sep 17, 2015 at 06:15 AM 0
Share

Is there a particular reason for using floats, rather than ints, for the clicks and autoClicks parameters? It seems like you always want those to be whole numbers, and floats are not guaranteed to be whole numbers, even when you think they should be. Floating point rounding errors are an extremely common headache and many developers prefer working with ints when possible for this very reason.

avatar image matthebest kingcoyote · Sep 17, 2015 at 04:24 PM 0
Share

because Time.deltatime is a float and if i use them as a int I will get an error.

2 Replies

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

Answer by Bunny83 · Sep 17, 2015 at 08:39 AM

The problem is most likely the order of execution of your code.

You first display the current "clicks" number, then you round it, then you add the autoClicks. Since your autoClicks is multiplied by deltaTime it will be a small value with a lot decimal points. You want to reverse those three lines:

  clicks += autoClicks * Time.deltaTime;
  clicks = mathf.round(clicks * 10f) / 10f;
  text.text = "" + clicks;


You also want to put those lines at the end of your Update method. Any changes that are applied to "clicks" should happen before you round it.

edit
Furthermore there's another potential problem with that approach. Since you round the actual clicks counter you "distort" the actual value. At a high framerate it would be possible that your autoclicks addition might have no affect at all if it's too small and is going to be rounded away each frame.

You should seperate the logic (the actual counting) from the display of the number. So only round it when displaying the number but leave the actual counter alone.

 clicks += autoClicks * Time.deltaTime;
 text.text = "" + (mathf.round(clicks * 10f) *0.1f);

For other ways to output a formatted string see @Helliums answer.

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 matthebest · Sep 17, 2015 at 08:44 PM 0
Share

Awesome!!! Thanks for the help.

avatar image
2

Answer by Hellium · Sep 17, 2015 at 07:17 AM

If you look at the documentation

http://docs.unity3d.com/ScriptReference/Mathf.Round.html

You will see that this function returns a float. Thus, there may be some imprecisions.

I see two simple options :

  • Use Mathf.RoundToInt function

  • Format the output string value using string.format( "{0:#}", clicks ) ;

EDIT : Integers won't be a good solution in fact, since you seem to make a clicking game with auto-clicks, floating values will be necessary.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Mathf.Clamp does not work at all. 1 Answer

Int not getting rounded data 0 Answers

Save object position after drag? 0 Answers

Mathf.Pow alternative function? 1 Answer

how do i Limit camera rotation on Y-Axis 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