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
0
Question by Tokyoscarab · Aug 25, 2015 at 02:29 AM · 2d gametransform.positionfloat

Floating Point Error?

Hi! I'm making a game in unity 2D and I'm having a floating point issue. I have an object that I want to shift down .2f for every input of its button. but the inspector window when I test it, I notice a problem: For reference, the code is: if (Input.GetKeyUp(KeyCode.Keypad0)){ gameObject.transform.position -= new Vector3(0f, .2f, 0f); }

So if the object starts at a Y of 6f, these are the Y positions: 6 5.8 5.6 5.400001 5.200001 5.000001 etc, then 0.600001 0.400001 0.200001 1.221895e-06

Why does it do this? It's position in the game is, I assume, 0.000001, but then why does Unity think it is at the given space? I'm just frustrated and would love some help on what I can do to fix this or a better way to go about it that is more reliable. I need to keep track of its position and have things happen at certain coordinate values. Thanks in advance! :D

Comment
Add comment · Show 1
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 getyour411 · Aug 25, 2015 at 02:41 AM 0
Share

There are many explanations of floating point already; could you the C# round to decimal precision function?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cjdev · Aug 25, 2015 at 02:48 AM

The problem is floating point imprecision. To get around it you can use Mathf.Approximately(float1, float2);. Also, Unity basically does the same thing behind the scenes for Vectors when you use the default comparison for them so you're covered there.

Comment
Add comment · Show 4 · 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 Tokyoscarab · Aug 25, 2015 at 02:45 AM 0
Share

I think my main concern is why does the float point change the displayed value so drastically when at or crossing the Y-axis? Especially when the number it is saying isn't the position the object is actually at

avatar image getyour411 · Aug 25, 2015 at 03:11 AM 0
Share

@tokyoscarab please do not post a comment or follow-up as an Answer; I converted this one for you.

avatar image Tokyoscarab · Aug 25, 2015 at 06:42 AM 0
Share

What would be a good example of using that comparison in the application I am trying to make?

avatar image cjdev · Aug 25, 2015 at 07:04 AM 0
Share

You could use it to compare two Y positions for instance to check for equality even though they have those extra decimal places.

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

26 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

Related Questions

C# function with multiple float arguments not working 1 Answer

For loop comparing two different objects appears to not be running 1 Answer

Having trouble teleporting player in a 2D game 0 Answers

Need 2D help - Want to have object move towards point when it touches a trigger. 0 Answers

Moving in Y and X after collision 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