• 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 Jakeoteph · Apr 09, 2017 at 05:24 AM · movementtransformvector3

How do I Round with Vector3.up

I have a character that moves on 32x32 tiles and when it moves, I use Vector.up in the inspector I get values like 0.1599999 instead of 0.16 how can I fix this? Here is my code:

  void Update () {
         buttonCooldown--;
 
         if (canMove) {
             pos = transform.position;
             move ();
         }
 
         if (moving) {
             if (transform.position == pos) {
                 moving = false;
                 canMove = true;
 
                 move ();
             }
 
             transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);
         }
     }
 
     private void move () {
         if (buttonCooldown <= 0){
             if (Input.GetAxisRaw("Vertical") >= 1){ 
                 if (dir != DIRECTIONG.UP) {
                     anim.Play("Stand Up", -1, 0f);
                     buttonCooldown = 10;
                     dir = DIRECTIONG.UP;
                   }   else {
                     canMove = false;
                     moving= true;
                     pos +=Vector3.up * 0.32f;
 
                    }


















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

3 Replies

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

Answer by toddisarockstar · Apr 09, 2017 at 07:09 PM

What is the problem? are you having problems when compairing?

         Vector3 rv = new Vector3 (0.1599999f, 0.1599999f, 0.1599999f);
 
         rv = new Vector3 (Mathf.Round(rv.x * 100), Mathf.Round (rv.y * 100), Mathf.Round (rv.z * 100))*0.01f;
 
 // this shows  (0.16,0.16,0.16) in the inspector

 





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
avatar image
1

Answer by NoseKills · Apr 09, 2017 at 02:45 PM

Short answer: You can't. You have to live with it. Floating point numbers have limited precision.

You are not really saying what kind of a problem this floating point precision "error" causes for you. The number you are seeing is practically 0.16 and 99% of the time this kind of errors don't cause any problems visually for example.

If it causes other kinds of logic problems, you just have to program around them.

For example if an object is supposed to be positioned on a grid with 0.5 unit wide cells, rather store the object's grid cell coordinates as integers, use these integers for game rules and position checking, and use transform.position (vector3 of floats) only for positioning the visual representation (gameObject) on screen - dont try to use transform.position for checking if an object is at coordinates (1,1) == (0.5f, 0.5f) because sooner or later some object will be at .49999... or at .5000...1.

Here's a pretty good video about why this is happening in general

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
avatar image
0

Answer by Cuttlas-U · Apr 09, 2017 at 04:51 PM

hi; I usually write an "if" statement to check the difference between the value and the final value and make it correct my self when the function is at the end ;

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.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to remove jitters from moving game object 1 Answer

Player moves diagonally even when not telling him to 0 Answers

Moving an Object - The right way. 2D 1 Answer

Move Obj A towards Obj B BUT keep going in same direction when Obj A has reached Obj B 1 Answer

Move from one position to another in x seconds 2 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