Search Unity

Duplicating gameobjects produces slightly different transform

Discussion in 'Editor & General Support' started by aliken9, Oct 8, 2014.

  1. aliken9

    aliken9

    Joined:
    Jan 28, 2013
    Posts:
    2
    Sometimes when I duplicate or switch parents for a game object, there is a slight offset in position and scale for the new object. An original position of (0, 0, 0) would turn into something like (0.00002834, 0.00002834, 0.00002834) and a scale of (1, 1, 1) would turn into (0.99999843, 0.99999843, 0.99999843). This happens quite often for NGUI, so I suspect it can be a float precision issue since the root parent is at a 0.002604167 scale, so calculations might lose precision.

    Has anyone else run into this problem before and is there a good fix around this? I find myself always fixing the same gameobjects over and over for nice whole numbers because Unity keeps deciding to shift things randomly.
     
  2. TeKniKo64

    TeKniKo64

    Joined:
    Oct 7, 2014
    Posts:
    30
    Having your objects parents to something with a scale of "0.002604167" is most likely the issue. I suggest editing the parent's scale outside of Unity and then reload the asset.
     
  3. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Sorry for waking up this thread, but Wow! This is still happening on 5.2.0f3!

    Here I am sharing with you the bug I uploaded to Unity explaining that this still happening and look what they answered!

    https://fogbugz.unity3d.com/default.asp?726873_b6h64obkv0f8151e

    Unity does not know how to do a copy/paste operation!

    I already gave them options on how to solve this:

    Math float imprecision happen when you are making a math operation, so here are 2 ways of getting a solution for this:

    1- What we are doing is a simple copy/paste operation so do NOT calculate anything, just use the original values since we are not even changing parent.
    2- If we have to calculate anything (due of some bizarre logic, are you sure you wanna do this when duplicating something?) just copy/paste AFTER doing all that silly math that is going on.

    Anyways, sometimes details like this ruin the fun and the whole wonderful experience I enjoy in Unity.

    I hope this gets fixed someday.
     
    Last edited: Sep 22, 2017
  4. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
  5. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Yes it is! The only way to fix it is to manually set every messed up value AND you have to set a floating point value in order to work, like 0.1, sometimes you even have to add several zeros after the coma, this is so wrongly done!!
     
    Shushustorm likes this.
  6. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    There is another way, too: If you have to set a value to 0, type in another value (1, for example), then, right afterwards, hit backspace and then enter 0. While being super awkward and not comfortable to work with at all, this does the trick.
    I sure don't know why this works, though.
     
    Harinezumi and lermy3d like this.
  7. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    This worked for me on Unity v5.2.0f3!! Thanks!

    Also are you aware of this next rotation bugs? they are probably related to this weird behavior!

    Let us try this: (Bug 1)

    1 - Create an empty scene.
    2 - Create a new GameObject and apply some rotation.
    3 - Save the scene.
    4 - Open the scene again and watch the rotation messed up values in the GameObject. (is a bug or unity is not showing actual values)

    Another thing with this Rotation bug is the following: (Bug 2)

    1 - Create an empty scene.
    2 - Create a new GameObject and apply some rotation. (EDIT: and change either position or scale values)
    3 - Duplicate the GameObject and watch the original GameObject rotation values! They are messed up.

    It is like trying to see the rotation values re-calculates them, did not knew that Unity was resembling quantum behaviour here, I am calling this the Schrodinger cat's Unity Bug! :D
     
    Last edited: Sep 18, 2015
  8. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Hi guys I implemented a CustomDuplicate script just add it into an Editor folder, then right click on any GameObject and press Custom Duplicate, for the position and scale everything is going to work fine, but until the previous rotation bugs that I mentioned just above gets resolved the rotation will continue to have this behaviour.

    Let me know how it went with the script, I will gladly receive any suggestion! :)

    EDIT: Including VectorExtended class.
     

    Attached Files:

    Last edited: Sep 18, 2015
    Harinezumi likes this.
  9. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    I was totally able to reproduce this.
    Unity seems to show the actual values, though, because after opening the scene again, the gizmo is different, too.
    Also, this bug requires some more conditions to show up: You need to have set local rotation and if you do this again (changing the values, save the scene, reopening it again), this problem won't show up anymore.
    This reminds me of the very annoying problem that I am already facing for several months: All entries of the color libraries that I create within a project will be deleted when reloading a scene. Afterwards, when I create the entries again and reload the scene once more, the entries stay there. So maybe there is a crucial bug concerning loading scenes? (for reference, the color library problem: http://forum.unity3d.com/threads/project-color-library-entries-lost.312465/ )
    Oh and one more thing about the bug you described: If you do the same with the rotations as I described before with positions (set another value, backspace delete, then set value you desire), the values will not be messed up either. At least not "completely". In my testing, I still end up with .00001 inaccuracies.

    This one is different for me. The original GameObject will still have the values I typed in, but the duplicated object will have imprecisions like .00002.

    Also, I should point out that while these bugs show up when using Unity objects, the rotations will stay totally fine if I'm using meshes stored in .blend-files. Maybe using prefabs will result in accurate results?

    EDIT:
    Yes, making a prefab of the GameObject (even if it's just an Empty) will store the rotations and transforms correctly. You can reload the scene and everything is fine then.
    It would be nice to have some official feedback on this, though. Maybe we are doing something wrong? Is it possible to save or load a scene incorrectly?

    EDIT2:
    @lermy3d
    I just tried using your script and get 3 error messages. Each of them says:
     
    Last edited: Sep 18, 2015
    lermy3d likes this.
  10. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Thanks for confirming this, I followed your suggested steps and is exactly like you said.

    How weird you cannot reproduce the second bug, I even tested in a friend's computer as well and this happened also. Which Unity version are you using?
     
  11. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    I'm using version 5.2.0f3 on OSX 10.10.2.
     
  12. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Hi! yess, sorry I forgot to inlcude the VectorExtended.cs, is added to the same post!
     
  13. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Hello Shushustorm, thanks to you I just realize on more step to always reproduce this bug. Here it goes:

    Transform duplicate mess up original object rotation

    1 - Create an empty scene.
    2 - Create a new GameObject, apply some rotation and change either position or scale values.
    3 - Duplicate the GameObject and watch the original GameObject rotation values! They are messed up.

    Let me know! :)
     
  14. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Alright, this seems to work! Thanks for the script! I didn't do that, though:
    It just works by itself when it's located in the Editor folder?

    EDIT:
    Now I'm not even able to reproduce the bug anymore, even after removing the .cs files and reloading the project.
    How does that work? I don't know!
     
  15. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Yes, the VectorExtended class does not need to be on an Editor folder, I place VectorExtended outside the Editor because I use it in my projects. For the case of the CustomDuplicate by just placing it inside an Editor folder you can see the option appear in the right click context menu.
     
  16. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Well, I actually removed both files from the project entirely. And it still works.
    I just wanted to see if copy/paste of the transform component would get rid of this bug, too.
    But now it's not showing up anymore.

    EDIT:
    I see. I have to right-click the object in the hierarchy. Yes, this works.
    But I cannot confirm if it works compared to "normal" duplicating, because right now, for whatever reason, the bugs are completely gone anyway. Even with normal duplicating.

    EDIT2:
    Even creating a new scene won't make the bugs show up. I'm creating another testproject now.

    EDIT3:
    Yes. With a new project, the problems are there again. That's weird though.
    So your scripts actually "cure" the project from those bugs even after removing them.

    EDIT4:
     
    Last edited: Sep 18, 2015
    WatchYourVector and lermy3d like this.
  17. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Hahaha! I really doubt so! It must have been something you have done in between! :D
    IF you do find it out! PLEASE tell me!!! :rolleyes:
     
  18. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Have you actually tried removing your scripts from your project and make the bugs show up again?
    Because I can't think of anything I might have done in between that could have made an impact on this.

    EDIT:
    Alright, now the bugs are gone from the new project also. Even though I didn't include your scripts. Are those, by any chance, sporadic bugs? I haven't done anything spectacular. Just adding empties, creating scenes and setting different rotation values on the empties.
    When it comes to UI, I do have those float imprecisions all the time, though.
     
  19. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Exactly! I tried to delete my scripts and did obtain the same results, and as for UI concerns is more easy to reproduce since they always happen.

    I was not able to clear the bugs away though :(. Perhaps by setting the values to the transform several times will make it to not recalculate the values for some reason! In the test project I also did the same operations as you, adding empties, create scenes and changing transform values, but without avail.
     
    Last edited: Sep 18, 2015
    Shushustorm likes this.
  20. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    For now, I just hope this gets official feedback. Because I really don't know more specific circumstances for the bugs to show up, so I can't seem to reproduce it constantly.
    I just tried using the UI system inside the new test project and even that won't make the bug show up all the time, which is weird, because in the projects that I'm working on, they really do show up consistently.
    Maybe projects made in Unity 5.2 will sometimes prevent those bugs.
     
    Last edited: Sep 18, 2015
    lermy3d likes this.
  21. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Well today they re-opened the bug report regarding this post where I also send them the custom duplicate script and comment on this 2 bugs, let's see what happens. I will keep you posted.
     
    Shushustorm likes this.
  22. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Alright, thanks for the bug report! Hopefully, they will get it fixed.
    EDIT:
    I read this is happening only to childed GameObjects? Well, the empties that I created, which showed the bugs, were not parented to anything. At least not to other GameObjects. Maybe to the origin, if this is an internal parent for all the GameObjects.
     
    Last edited: Sep 18, 2015
    lermy3d likes this.
  23. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Guys, check out the response in the reported bug for Transform values preicision issue when duplicating GameObject :

    "I spoke with our developers team and they have decided that this particular issue will not be fixed."

    Unbelievable answer from the Unity Development Team!!
    They cannot return a rotation without messing up the values!
    This is just too much!
     
    Shushustorm likes this.
  24. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    While this is kind of disappointing, it still is their decision what to fix and what not to.
    It's a pretty annoying bug, yes. Especially when you are creating hundreds of UI elements and you have to go through them and correct most of their float imprecisions. But at the end of the day, they probably won't change this unless an outrageous number of people would complain about this, which makes sense when you think about UT's thoughts on democratizing game development. And that's not necessarily a bad thing overall. In this case, though, we just may be out of luck.
     
  25. enchiridion

    enchiridion

    Joined:
    Oct 29, 2014
    Posts:
    57
    Wow this is STILL an issue in 2017 5.4.1f1!!!

    When I duplicate a game object:


    This bug is 1 of 4 that I wrote about in this thread here.
     
    lermy3d likes this.
  26. radiowaves

    radiowaves

    Joined:
    Jan 20, 2017
    Posts:
    12
    Still happening in 5.6, and that version also broke most of the UI, percisely due to float calculation errors. Small things can grow to big problems, I wonder if this has something to do with it.
     
    lermy3d likes this.
  27. u-i

    u-i

    Joined:
    Nov 11, 2014
    Posts:
    5
    Yah, still happening, is really annoying hope they can fix this :)
     
    lermy3d likes this.
  28. LilGames

    LilGames

    Joined:
    Mar 30, 2015
    Posts:
    570
    Still happening in 5.6.3f1

    Select Duplicate on a UI sprite (or CTRL-D) and the scale instead of 1,1,1 is something like 0.9999997
     
    NicolasKtm, poukoute and lermy3d like this.
  29. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    Still happening in Unity 2017.3, and will never be fixed (read lermy3d's bug report that got "resolved")
     
  30. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    Is it really a problem though?
    Every transformation will lose some precision (basically 0). And yeah transform is re-multiplied by all parent matrices down to the common root if you switch parent, so some loss of precision is expected.
     
  31. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    I do think it is a problem that you have an object with position (0, 0, 0) and scale (1, 1, 1), you hit ctrl+d to duplicate, and now you have an object with a position of (1.42332-e06, 2.0491-e06, 0) and scale (0.999, 0.999, 0.999). Apart from looking horrible, other developers will not know what these values mean, if they are intentional or not, and if the slightly off values get duplicated enough times, they will not be slightly off anymore.

    I understand Unity developers' point that really fixing it would mean redesigning the whole system, but just to fix the duplicate functionality was done by lermy3d with simple Editor scripts.
     
    LilGames likes this.
  32. enchiridion

    enchiridion

    Joined:
    Oct 29, 2014
    Posts:
    57
    Yes it is a problem when it comes to animating. I don't want a key frame on 0.99343 I want a keyframe on 1.

    Also, if Maya, 3DSMax, Blender and countless other 3D programs can do it, then I don't see why Unity can't do it either. When duplicating it should not recalculate the scale, it should copy it from the original object instead. Simple.
     
    lermy3d and Harinezumi like this.
  33. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    I don't disagree that Unity is inaccurate but you're exaggerating.

    @Harinezumi
    The error really isn't that significant as you make it seem. it's at least 6 correct decimals before the inaccuracy.
    And copy (ctrl-d - duplicate) does more than just duplicate an object in memory. It then re-parents it to the scene/parent.
    That means calculations -> 4x4 matrix calculations -> 4x4 matrix multiplication with floats.
    Floats are inherently inaccurate. https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems

    @enchiridion
    Maya and 3DSMax also experience same rounding errors, except you can hide them.
    http://help.autodesk.com/cloudhelp/...GUID-94CA1C2A-B027-420B-ACE0-66FADF1DC676.png
    Blender also experiences the same errors, but it hides them until you view them in, say, an exported .obj file.
    https://www.blender.org/forum/viewtopic.php?t=26122

    It's matrix multiplication. All values get touched and dirtied.

    Float precision errors plague every 3D software that uses them. You can't escape them. You may only keep your scene close to 0,0,0 to minimize their effects.
     
  34. enchiridion

    enchiridion

    Joined:
    Oct 29, 2014
    Posts:
    57
    That's what I am trying to say. AFTER all the reparenting and matrix multiplication it should just literally ctrl+c ctrl+v the values of the original object to the newly duplicated one, exactly the same way I do it when I "fix" the values. Problem solved. I honestly do not know why they don't do that.
     
    lermy3d, LilGames and Harinezumi like this.
  35. poisonnuke

    poisonnuke

    Joined:
    Nov 22, 2016
    Posts:
    82
    There are two things:

    If I reparent any object inside the hierarchy, all values stay _exactly_ the same as before. But in this case, an actual recalculation is neccessary. So why does it work during moving an object inside the hierarcy?

    And there is no use/sense at all to recalculate anything during duplication. It is just useless. There is no logic behind that, which means the devs have no idea what they are talking about.


    And fixing he issue is nothing more than a 5min job. I dont know the source of unity, but dont need to. Just store the old values and insert after instantiation. Ists just that easy, regardless what is happening around. Every capable dev can do this during coffee break, without ever seeing the source of unity itself.
     
    LudiKha and enchiridion like this.
  36. poisonnuke

    poisonnuke

    Joined:
    Nov 22, 2016
    Posts:
    82
    there is one more thing:

    If you duplicate objects and try to use mass-edit (which is crap btw compared to tools like C4D), you cant tell which value was originally the same and which not.
     
  37. KevinGelking

    KevinGelking

    Joined:
    Jul 17, 2014
    Posts:
    15
    Funny that this is still happening in the 2018 beta...
     
  38. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    Unfortunately, it's not funny, and I don't think this will be fixed. There is serious resistance against it, even though it seems the "copy values" approach that poisonnuke suggests would work.
    This is exactly what my issue is: why does it work with reparenting, but not with duplicating?
     
  39. aklgupta

    aklgupta

    Joined:
    Jun 9, 2014
    Posts:
    29
    This is a long present issue, and can't believe it still hasn't been addressed.
    Currently this same issue is really messing up my current project as it uses a lot of nested UI elements, and in my case I need to duplicate a lot of them due to similarities b/w them.

    However, in the recent instance of this bizarre occurrence, the difference in the expected and actual value is no longer small enough to be not noticeable.
    I create a image based divider in a form. And when I duplicated it, the scale changed to 1.000578 from 1, which, for the given resolution is very noticeable. A diff of 0.0578% is pretty huge imo.

    It gets even worse when copying a GO and pasting it under a different parent, usually in another scene.

    Using Unity 2018.3.0

    @lermy3d, thanks for those scripts. I'll give them a shot.

    Update: Is anyone facing similar issue with the snapping with the various transform tools?
     

    Attached Files:

    lermy3d likes this.
  40. enchiridion

    enchiridion

    Joined:
    Oct 29, 2014
    Posts:
    57


    Firstly, your scale is not 1 for your game window.
    Secondly, your game window is extremely small, try maximizing, setting the scale to 1 and see if the difference is still noticeable, I don't think it will be.

    why does it work with reparenting, but not with duplicating?
    Very good point. There should just be another shortcut like ctrl+shift+v which pastes the object with it's original transform values instead of recalculating for the new parent.

    SIMPLE.
     
    lermy3d likes this.
  41. aklgupta

    aklgupta

    Joined:
    Jun 9, 2014
    Posts:
    29
    Thanks for the suggestion. I tried the free aspect as well, but it still persists. However, it was later that I noticed that the difference is much less noticeable in the play mode. Some how the Game window, while not in play mode, makes it look worse than it really is. Regardless, I have removed the second line, and gone with just one to avoid unexpected behavior.
     
  42. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I noticed in the changes for 2018.3.3 that this issue is supposed to be resolved.

    https://unity3d.com/unity/whats-new/2018.3.3
     
    lermy3d and aklgupta like this.
  43. aklgupta

    aklgupta

    Joined:
    Jun 9, 2014
    Posts:
    29
  44. rayD8

    rayD8

    Joined:
    Mar 7, 2018
    Posts:
    32
    I just came here looking to see if the issue was resolved, because I'm not running into it any more when doing our UI work. I was confused, because we have been burned bad by it. A teammate that is new to Unity and was expecting it to... well work as one would expect when copy/pasting. I had to spend hours going thru dozens of screens and UI elements - correcting scales that went from 1,1,1 to driffftttttttt.

    I just updated the app to the newest version of Unity due to weird issues happening with another version of 2018. (those went away too). I'm using 2018.3.5f1

    Thanks for posting the issue tracking link.
     
    Last edited: Feb 25, 2019
    lermy3d likes this.
  45. xelanoimis

    xelanoimis

    Joined:
    Oct 29, 2013
    Posts:
    39
    says here it's marked as "Fixed in Unity 2019.1"
    https://issuetracker.unity3d.com/is...ified-recttransform-scale-and-position-values

    I've moved to 2018.3.8f1 recently, but didn't got the chance to see if still happening.

    However the whole thing is annoying.
    And I'm surprised how easy developers are ready to accept this, just because they know what's behind the tech.
    Guys, this is a game editor intended for humans, not a science tool to explore the behavior of float numbers!
    I would expect it to round those small errors when it presents them to the user in order to make life easier.

    I duplicate a value of 1 and get 0.9999997 that's crap.
    Should I remember that 0.9999997 == 1?
    Then why don't we get a 0.9999997 when we type 1 in inspector (rethorical)

    On the same line of ideas, if i do this:
    obj.transform.rotation = Quaternion.Euler(0, -90, 0);
    it shows in inspector -90.00001
    and clearly obj.transform.rotation.eulerAngles.y != -90.0f
    (which is true even if the inspector shows -90 set by hand)

    And if you suffer from the slightest form of OCD, you'll spend a lot of time making these values beautiful again ;)
     
  46. xelanoimis

    xelanoimis

    Joined:
    Oct 29, 2013
    Posts:
    39
    I've found a solution to the rotation issue. Apparently you can set the value from code directly in the inspector (I'm talking about editor tools here). I think it's available since 2018.

    Vector3 r = UnityEditor.TransformUtils.GetInspectorRotation(obj.transform);
    r.y += 90;
    UnityEditor.TransformUtils.SetInspectorRotation(obj.transform, r);

    No more -90.00001 or -89.999999 :D
     
  47. LilGames

    LilGames

    Joined:
    Mar 30, 2015
    Posts:
    570
    Still happening in 2018.4.1f1

    Why is this a problem? Workflow.
    People forget to check scale values when duplicating objects in the hierarchy. They then go on their merry way, animating and creating additional content with said objects. Then here you are a couple months later, dozens of animations later and you stare at a character's leg and think... "Why does that right leg look smaller than the left one?". OH the damn scale is off to weird values. Now we gotta fix it in the base character AND revise every single animation!!
     
  48. unleech

    unleech

    Joined:
    Jul 27, 2013
    Posts:
    17
    still happening in 2020.1.4f1 lol
     
    MurphyMurph_21 likes this.