• 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 ChristmasEve · Jan 01 at 07:13 PM · transform.positionchild objectworld spacetransform.parentabsolute

Place child GameObject at absolute world position

I've been developing in Unity for years and I can't believe I'm stumped by something that should be very simple. I want to instantiate a GameObject and place it at a world position but I also want to parent it to a twisted mess of a tree of GameObjects (all different rotations/scales of the objects in the tree). Seems simple enough. The Unity Editor can figure it out. I can spawn the object but not parent it, run it in the editor, hit pause and then drag the GameObject to the node I want and, lo and behold, it stays put.

But programmatically? That's been a nightmare. I've tried setting the parent transform right IN the Instantiate call. I've tried transform.SetParent() with a true for keepworldposition. I've tried TransformPoint. I've tried a half dozen other things besides this too, and the object keeps moving on me. I do not know the relative position for that complex transform but I also don't want the GameObject to not be parented because I want it deleted when the root is deleted.

Is it even possible to parent a GameObject to somewhere in the bottom of a twisted mess of an object tree but make sure it starts off at an absolute world position? Of course I don't care that the object would move with the parent. In fact, the parent won't be moving. I can't even set it's initial position properly.

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
0
Best Answer

Answer by sacredgeometry · Jan 01 at 07:20 PM

EDIT

This does appear to be broken in that version of Unity. I suggest you report it as a bug and roll back to a previous version

EDIT B

On testing it seems to be fixed in the last LTS version and the latest non LTS versions. So it is likely that it is just this version that has this problem.


transform.position = new Vector3(...)


Description: The world space position of the Transform. The position property of a GameObject’s Transform, which is accessible in the Unity Editor and through scripts. Alter this value to move a GameObject. Get this value to locate the GameObject in 3D world space.


https://docs.unity3d.com/ScriptReference/Transform-position.html


I'm not sure what the confusion is here, you can always set its position relative to the world. If its un-parented and you parent it (I think) it retains its position in world space it just updates its local space coordinates relative to its hierarchy.

Video

https://www.youtube.com/watch?v=MsLrajJXTl4&ab_channel=sacredgeometry

Comment
Add comment · Show 19 · 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 ChristmasEve · Jan 01 at 07:31 PM 0
Share

This actually does not work. That was the first thing I tried, hours ago. Just myObject.transform.position = myPos right after I parented it. It ends up below the ground in the different direction from the parent that it should be. It's in the general area but NOT at myPos.

Perhaps I'm using a buggy version of Unity? I'm using 2020.1.17f.

avatar image sacredgeometry ChristmasEve · Jan 01 at 07:35 PM 0
Share

Please see the video I just uploaded .. is that not the behaviour you are looking for?

avatar image ChristmasEve sacredgeometry · Jan 01 at 07:54 PM 0
Share

Thanks so much, I didn't realize you created a video just to answer my question. That was nice. No one has done that before. Okay, if I'm understanding it correctly, when you run it in the editor, node #3 is moving to the world origin via the code "this.transform.position = new Vector3()", which I know is 0,0,0 since C# automatically initializes all values to be zero.

So this is what I'm looking for, yes. But if it works at 0,0,0, why wouldn't it work somewhere else in my world? I tried setting the transform.position (not localPosition) to the absolute location in my world (like 575,50,230 or whatever it was - it's determined programmatically). The problem is, whether I parent it first or after setting the position, it will not stay there when the object is parented to my hierarchy of objects with all different scales and rotations.

Show more comments
avatar image ChristmasEve · Jan 01 at 08:30 PM 0
Share

sacredgeometry, I don't see my most recent comment in the hierarchy above. If I click "Show $$anonymous$$ore Comments", it stops with your comment about testing with other values. Does answers.unity.com have the same problem as GameObjects' positions?? LOL But I didn't see that comment until I wrote this whole thing but I'll post it again anyway. Thanks for looking into it :) Yeah, it's very counterintuitive behaviour.

Here's my original reply that is disappearing:

Thanks for even testing it in my version. I hope you didn't have to download it just for that quick test.

This is SO weird. Here's my code:

  var bobber=(GameObject)Instantiate(Resources.Load("bobber"));
  //        bobber.transform.position=pos;
  bobber.transform.parent=fish.line.transform;
  bobber.transform.position=pos;

//old code, tried it this way too...

// var bobber=(GameObject)Instantiate(Resources.Load("bobber"),pos,Quaternion.identity,fish.line.transform);

So, if I simply instantiate the object on its own and set it to the calculated position it needs to be it, it stays at: 2783.991f, 175.7f, 4639.482f (the correct location;) But parenting it to my hierarchy moves it to: 2809.843f, 170.3355f, 4645.75f Well, the position in the editor showed a local position so I hit pause, dragged it out to the root and then copied down this position.

This is driving me crazy. I have no scripts that are altering this object's position after the fact. Are you aware of anything that could be in the hierarchy of objects I parented it to that could throw off setting transform.position? I know you don't know my hierarchy (it's a U$$anonymous$$A character's bones), but can you think of anything that can throw off Unity's transform.position function?

I appreciate all the help you've given me.

avatar image sacredgeometry ChristmasEve · Jan 01 at 08:42 PM 0
Share

I have moved your answer to a top level comment and not to worry downloading it didnt take all that long.


So running the same tests in the version you are running seems to indicate its always being set to its parents root position i.e. Vector3(0,0,0). I have absolutely no idea why that is but its definitely not normal behaviour for every version previous to it and doesnt seem to be documented to have changed.


There is a more recent version 2020.2.1f1

I am downloading it now to see if its fixed in that.

avatar image ChristmasEve sacredgeometry · Jan 01 at 08:53 PM 0
Share

You'll probably have the answer before me because I just realized I don't ever want to upgrade until I back up my entire game first, and it's huge (24GB compressed). Starting to compress my whole project directory now....

avatar image sacredgeometry ChristmasEve · Jan 01 at 08:43 PM 0
Share

Oh just an aside unless you are just playing around I would suggest not working on any non Long-term support (LTS) versions as they are liable to maybe be broken or more likely for things to change.

avatar image ChristmasEve sacredgeometry · Jan 01 at 08:50 PM 0
Share

It think what I did last month was I downloaded the latest stable version at the time. I should upgrade while you are doing it. I'm always hesitant to upgrade often because it often breaks my code and I like to take care of a whole bunch of issues at once instead of incrementally. Perhaps, incrementally is better though because it's less overwhelming :) I guess the reason is I'm usually in the middle of something and don't want to fix extra bugs introduced by Unity, or deprecated behaviours or functions.

With this bug, I'm surprised I haven't witnessed lots of other weirdness in my game, like missing objects or objects located where they shouldn't be. That's what really surprises me.

Show more comments
avatar image
0

Answer by ChristmasEve · Jan 02 at 08:10 PM

I fixed this by having the bobber on the end of my fishing line be a separate, unparented entity and keeping a reference to it in a data structure so I can kill it when the line disappears.

There is either a bug in all recent versions of Unity (at least since 2018) or object.transform.position isn't meant to set a child object to an absolute position in the world. The final position is influenced by all of the scales and rotations in the hierarchy of parents, parents of parents, etc, under which the object sits. The only solutions are to keep the object at the very top-level (global level), parented to an object at 0,0,0 scale 1,1,1 rot 0,0,0, or to parent it to the root of your object as long as that object doesn't move or turn, of course (which was my case).

The is the only acceptable answer so far. I jumped the gun on accepting the other answer but he was extremely helpful and very appreciated.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

112 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

Related Questions

bullet's flight adjusts position according to a parent object ?? 1 Answer

transform.position is wrong when parent has scale different than Vector3.one 0 Answers

Picking up object with transform.parent does not work 1 Answer

Changing Child objects' position 1 Answer

Trying to position a world space menu in front of the camera whilst ignoring the camera's tilt angle 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges