• 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 /
This question was closed Feb 01, 2015 at 06:52 PM by Glurth for the following reason:

Multiple good answers.

This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Glurth · Jan 20, 2015 at 04:25 PM · transformlocalpositionparent transform

Why does assigning a parent to Transform change the localPosition?

EDIT: This question is now Obsolete, because assigning a parent in this way will now (as of 4.3.6f1) generate a warning in the Unity console.

 Debug.Log("new tooth position a: " + newTooth.transform.localPosition.ToString());
 newTooth.transform.parent=radialTransform;
 Debug.Log("new tooth position b: " + newTooth.transform.localPosition.ToString());

Debug output:

 new tooth position a: (0.0, 0.0, 0.0)
 UnityEngine.Debug:Log(Object)
 DynamicGearTeeth:Awake() (at Assets/DynamicGearTeeth.cs:45)
 
 new tooth position b: (-0.5, 0.0, 0.0)
 UnityEngine.Debug:Log(Object)
 DynamicGearTeeth:Awake() (at Assets/DynamicGearTeeth.cs:47)

I could understand if setting the transform's parent changed the newtooth's transform.position, but why is it changing the newtooth's transform.localPosition?

I did not include the code where I generate radialTranform and newtooth, because based whats wrong with that output, they dont seem to matter.

Comment
Add comment · Show 2
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 PorkMuncher · Jan 20, 2015 at 04:44 PM 0
Share

You can set the local position directly with transform.localPosition.

So you can after parenting the object do something like transform.localPostion = transform.postition.

Under the hood - for the engine - there is no such thing as a local position.

avatar image Glurth · Jan 20, 2015 at 10:09 PM 0
Share

transform.localPostion = transform.postition

That doesn't actually work. The parent has many transforms that are applied, I would need to INVERT all of those transform too. So, I just stored it in a local Vector3 variable , assigned the parent, and re assigned the localPosition with the value I stored.
U

nder the hood - for the engine - there is no such thing as a local position

I kind of agree: The localPosition value obviously exists in the transform; but I agree localPosition is definitely NOT a position, it is technically a "Linear Translation Transform". THIS is one of the reasons why I thought the value would not be changed on me when setting the parent. What I wanted to tell it by assigning a parent is: just apply these parent transforms on top of the existing local ones.

3 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by PorkMuncher · Jan 20, 2015 at 04:29 PM

When setting the parent of a transform the world coordinate stays the same - therefore the local coordinate is changed.

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 Glurth · Jan 20, 2015 at 04:37 PM 0
Share

Hmm, so if I want to insert a model into my transform-heirarchy (and keep the model's transform), I need to copy out all the transform data, assign the parent, the copy back all the transform data? (ah, this also explains why I find myself "resetting" those trasforms in the editor. I should have realized the answer from that!)

That seems like an odd choice. What am I missing? When would someone want to assign an object to a transform heiarchy, but keep it in the same world-position? I feel like THAT should be the special case.

P.S. please convert your comment to an answer, I'll accept it.

avatar image
2

Answer by christophfranke123 · Jan 20, 2015 at 04:57 PM

When setting a parent, Unity developers decided to keep the global position constant, which is represented by the transform.position variable. To be consistent, the localPosition of the new child has to be updated, because (in the simplest case with no rotation and scaling) the global position of a child is the sum of its localPosition and all its parents localPosition properties.

Comment
Add comment · Show 2 · 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 Owen-Reynolds · Jan 21, 2015 at 03:28 AM 0
Share

They didn't quite decide "to keep global position constant."

$$anonymous$$odelling programs have been using parents the same way as Unity, long before Unity, and everyone "knows" setting parents doesn't move things. Unity really just decided to copy the standard parenting rules.

avatar image Glurth · Jan 21, 2015 at 08:45 AM 1
Share

Well, everyone except Glurth knew. NOW, everyone knows.

avatar image
0

Answer by Owen-Reynolds · Jan 21, 2015 at 03:24 AM

When you give an object a parent, there are two common cases:

1) You already placed the object where you want it. Maybe you just found it easier to set the "real" position of everything, then parent them all together. Or maybe it just hit the parent and you want it to stick right where it is.

In that case, assigning the parent w/o moving the object is perfect.

2) You want to specially place the object in relation to the new parent. Maybe you have a spinning tableTop that you want to suddenly sprout a leg you're just now Instantiating.

In that case you first give it a parent and then position it by setting localPosition. Ex: leg.localPosition=new Vector3(-1,0,-2); You usually also set localRotation in those cases.

In any case, localPosition is always from the parent. So, whenever an object gets a parent, or changes parents, localPosition has to change, to be how far you are from the new parent.

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 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

Related Questions

Code does not work 1 Answer

GameObject position and localPosition not changing in hiearchy, only in script. 0 Answers

Issue with moving character along his/her local z axis when space key is pressed. (Parkour) 1 Answer

Finding the forward vector of a child in local space? 3 Answers

Problem assigning transform variable to prefab. 1 Answer


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