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 SpectreCular · Jan 22, 2015 at 01:42 PM · rotationtransformpositioninspectorfloat

Floating-point errors while assigning transforms

In my hierarchy, I have:

  • Main camera

  • Empty game object named "Level"

I've also created and attached a simple script to Level which is meant to load dungeon tile prefabs, generate a tilemap, and lay out the tiles in 3D space to make a seamless dungeon. The problem is, whenever I programmatically assign a position, rotation or scale to any given tile, I get massive floating-point errors between 0.00001 and 1.3, which leaves a visually noticeable unaligned tile layout.

I have even tried explicitly casting these values into integer values, checking in the debugger that they are indeed the expected values, and yet, after assigning them to a transform, they still show up slightly off in the inspector. However, when launching the script and then manually setting these values within the inspector, all the tiles line up perfectly and everything looks as expected. So the issue only occurs when assigning values to transforms within a script...

What is the cause of this madness, and what can be done to fix it?

Comment
Add comment · Show 6
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 christoph_r · Jan 22, 2015 at 10:09 PM 0
Share

What sort of values do you have for the positions to get such huge floating point errors?

avatar image DanSuperGP · Jan 22, 2015 at 10:18 PM 1
Share

To get floating point errors as large as 1.3, you've got to be generating huge maps. Perhaps scale everything down an order of magnitude?

avatar image khos85 · Jan 22, 2015 at 11:04 PM 0
Share

Do I understand correctly that int has a better behaviour,, I wonder if rounding off the float value could help?

Can you show the error where you state "massive floating-point errors between 0.00001 and 1.3"?

avatar image christoph_r · Jan 22, 2015 at 11:08 PM 0
Share

It's odd that it behaves better when the values are entered in the editor. But as long as transform uses floats for position, feeding it an int won't make a difference because it will be implicitly converted to a float, which then again leads to floating point errors.

avatar image SpectreCular · Jan 23, 2015 at 04:22 AM 0
Share

$$anonymous$$y tile map is a simple 5x5 set of wall and floor prefabs, each of which are no larger than 6 Unity-standard units in the X and Z axes. The largest value entered into the inspector was a local Y-rotation of 180.0, which, when explicitly set in code, becomes 178.7 in the inspector. Oddly enough though, when attempting to recreate the issue in a $$anonymous$$imalistic new project, I couldn't reproduce such a large margin of error. If I can find a way to reproduce this behavior, I will post a new comment about it.

Show more comments

1 Reply

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

Answer by SpectreCular · Jan 23, 2015 at 07:11 AM

Since manually setting the transform in the inspector always seems to fix this issue, I have found a temporary solution for anyone else who might be looking for one...

  • In the project explorer, select any prefab (such as a wall)

  • Hold the Alt key, and drag the prefab into the same folder. This will create a copy.

  • Rename your copy using whatever naming convention works for you (I just did "wall_0", "wall_1", etc)

  • For "wall_1", set the Y-rotation to 0 in the inspector. Increment this value by 90 for "wall_1", then repeat this process until you have 4 prefabs, each with a Y-rotation 90 degrees higher than the previous copy.

.. Now you can instantiate your prefabs in any script, simply using whatever naming convention you chose, without having to worry about floating-point errors messing up your transforms. Unfortunately, this solution only accounts for rotations. If floating-point errors are visually messing up your translations, you will need to find some other solution.

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 SpectreCular · Jan 24, 2015 at 02:18 AM 0
Share

I'd like to add that assigning integer literals or constants (NOT floats) to a transform also seems to correct the issue... But of course, doing this could potentially limit how dynamic your code could be. Sometimes, if it means gaining functionality, the trade-off isn't so bad.

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

6 People are following this question.

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

Related Questions

Accurate placed object's transform messes up on game start 1 Answer

How to make Camera position Independent of its Rotation? 1 Answer

setting transform physics2d issue 0 Answers

Align objects based on child objects 0 Answers

Ragdoll breaks and flys far into the air... 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