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 /
avatar image
0
Question by MartinTV · Nov 26, 2019 at 05:54 AM · guitransformpointshandlesinversetransformpoint

Gross "Almost zero" Vector components when adjusting to local space and custom handles

Hey gang,

I am drawing some handles to display a Vector3 relative to a GameObject, think of moving a child object and how it's position in the inspector is relative to the parent.


The handles I am drawing manipulate the elements in a Vector3 array that the object has. These vectors need to be relative to the object and display as such (again think of moving a child object and how its own inspector displays the position as relative to the parent) and I specifically don't want to have it reference a bunch of empty child game objects (personal challenge for me to up my game as a programmer).


What I am using is a combination of InverseTransformPoint and TransformPoint to place the handles in the scene but keep the value as a relative position. I feed the position into the Handle.PositionHandle using TransformPoint so it is drawn relative to the object in question, then that is wrapped in an InverseTransformPoint to get it back to a form that is relative to the parent.


NOTE: "b" is a reference to a class that stores the vector I want the handles for as well as other information (its name starts with a "b" so that is what I am using in code, I wanted to explain that in case you were expecting a 'v' for some standard vector) and "t" is just the transform of the object that these vectors are relative to.


 b.position = t.InverseTransformPoint(Handles.PositionHandle(t.TransformPoint(b.position), t.rotation));


So this ALMOST works, the problem is that when I start rotating the main object, and the vector 3 is then moved relative to it, its XYZ components in the Inspector change by ludicrously small amounts that are essentially zero (I even see a few decimal places with an e in the mix).


I just want it to be as nice and clean as if I had a child object, none of this decimal kerfuffle. The following image should help.


alt text

explanation.png (110.5 kB)
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
2
Best Answer

Answer by Bonfire-Boy · Nov 26, 2019 at 05:10 PM

I really don't like the idea of changing the data as per steve_thud's answer.

An alternative would be to write your own inspector for the Transform class and do the rounding only on display. One could/should add the option to switch to showing the precise values, and only enable editing when those are being shown.

Creating a new inspector for Transform can be really useful. My current project, for instance, has one which shows World as well as Local position/rotation values, and has drag/drop targets enabling me to copy the position of a game object or other asset (I'm using lots of scriptable objects that define objects' positions).

Comment
Add comment · Show 5 · 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 MartinTV · Nov 26, 2019 at 10:21 PM 0
Share

Oh cool, great idea! I will try it out!

But while we're talking, was my method for calculating the local position the "right" way? I can't help but feel that even with the floating point rounding errors steve-thud mentioned, there has got to be a way for me to do it and I'm just lacking the know-how.

avatar image Bonfire-Boy MartinTV · Nov 27, 2019 at 09:59 AM 0
Share

I'm not sure what you mean. The only problem with those numbers is in your perception of them. That's not to belittle the issue - it is handy if zeros stand out as such. But it's also important to understand that floating point "errors" are things that you have to live with rather than try to make go away.

avatar image MartinTV Bonfire-Boy · Nov 27, 2019 at 11:22 PM 0
Share

Hey, fair enough. Just ruling out the possibility that I was doing sub-optimally.

Thanks again :)

avatar image Bunny83 MartinTV · Nov 27, 2019 at 08:44 PM 0
Share

Actually your issue is that you do not actually check if there actually was a change due to the handle but you always convert the position to worldspace and back each time and replace the original value regardless. You should do:

 EditorGUI.BeginChangeCheck();
 Vector3 newPos = Handles.PositionHandle(t.TransformPoint(b.position), t.rotation);
 if (EditorGUI.EndChangeCheck())
 {
     b.position = t.InverseTransformPoint(newPos);
 }
avatar image MartinTV Bunny83 · Nov 27, 2019 at 09:34 PM 0
Share

I made those changes but I still get the same rounding issue. But in general, I should do it this way in the future?

Additionally this message appears in the inspector:

"Should not be capturing when there is a hotcontrol"

avatar image
1

Answer by steve-thud · Nov 26, 2019 at 02:24 PM

These small changes in position are caused by floating point rounding errors. Unity is doing its best to calculate the position of the object, but due to computing limitations its going to be a little off by an insignificant amount, as you are seeing here.

If you're really committed to having fewer decimal points show up in the inspector, and you're already setting the position through code, then you can try this:

 int numberOfDecimalPoints = 2;
 Vector3 newPosition = t.InverseTransformPoint(Handles.PositionHandle(t.TransformPoint(b.position), t.rotation));
 
 newPosition.x = System.Math.Round(newPosition.x, numberOfDecimalPoints);
 newPosition.y = System.Math.Round(newPosition.y, numberOfDecimalPoints);
 newPosition.z = System.Math.Round(newPosition.z, numberOfDecimalPoints);
 
 b.position = newPosition;
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 MartinTV · Nov 26, 2019 at 10:11 PM 0
Share

For the short term, this is what I am going to do (certainly this will be the solution for some other stuff I am working on) but I might go with making the custom inspector, but still thanks for the answer!

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

230 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 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 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 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 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 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 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 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 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

Intercepting Position Handle in Game Scene? 1 Answer

How to set the position of a guitext using transform? 1 Answer

How to create a point/score system based on player performance. 2 Answers

Using Handles at OnGUI 1 Answer

GUI item with object position+dimensions 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