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 AppTechStudios · Nov 09, 2012 at 06:11 AM · parentunparenting

Unparenting Changes Object's Location

I have an several objects as a children of a parent object and I want to unparent 2 of the children.

 transform.parent = null;

The problem is that when I unparent the two objects, they instantly move to the global coordinate equivalent of their local coordinates (let me explain).

Let's say that the parent object is located at 100, 100, 100. Also, let's say that both the children objects are at 1, 1, 1 (which is relative to the parent). When the two children are unparented, they move to 1, 1, 1, but now they are global coordinates.

The result is that as soon as the two children are unparented, they instantly move to a unwanted location. The goal is to keep them in the exact same location as before they were unparented. Is this possible?

I realize that I did not explain very well, let me know if/how I can clarify.

Thanks.

Comment
Add comment · Show 5
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 Seth-Bergman · Nov 09, 2012 at 06:25 AM 0
Share

do they have some sort of script setting their position?

avatar image Eric5h5 · Nov 09, 2012 at 06:29 AM 2
Share

That's not something that happens by itself; unparenting doesn't change any object's location unless you're doing something to make that happen.

avatar image AppTechStudios · Nov 09, 2012 at 07:25 PM 0
Share

I don't have any code besides the line above. If you just look in inspector when the objects are unparented, it looks like nothing happened (the coordinates stay the same). But when the objects are unparented, the coordinates seem to become global coordinates ins$$anonymous$$d of relative to the parent object.

This causes them to instantly move to another (unwanted) position when they are unparented.

Am I doing something wrong?

avatar image Eric5h5 · Nov 09, 2012 at 07:42 PM 0
Share

I would say yes, but I don't know what. I haven't seen any behavior like that under any circumstances.

avatar image kaliAJ · Jul 15, 2020 at 09:43 AM 0
Share

@AppTechStudios @Seth-Bergman @Eric5h5
I am facing the same issue please help if you found any solution to this. Basically I have a prefab which has my "player" & a "PlayerController" as a childobjects to the prefab. playerController is basically 6 UI buttons that control the direction of the player by using the onClick events. but dont know why this setup doesn't work when the "player" and the "playerController" is childed to some parent. onClicking the UI buttons with the same scenario make the player move indefinitely in opposite direction. but this dosent happen when player and playerController are independent objects... please tell me WHY????? this is why i am trying to unparent this 2 things form the prefab instance.

5 Replies

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

Answer by AppTechStudios · Nov 11, 2012 at 05:26 AM

I found the solution. Oddly enough, I simply had to uncheck the "Play Automatically" box in the animation component of the parent object. Now, when the children are unparented, they stay in the same spot as expected.

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
avatar image
1

Answer by DaveA · Nov 09, 2012 at 09:30 PM

That is odd, but you could force it like this:

 Vector3 temp = transform.position; // world pos
 transform.parent = null; // *should* not move, but you say...
 transform.position = temp; // restore world position
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 AppTechStudios · Nov 11, 2012 at 05:05 AM 0
Share

First off, I couldn't get the first line to work (it errored so I replaced the Vector3 with var and the error went away). I tried checking the unparented object's positions just after I unparented them and the positions are different from those shown in inspector.

So what happens in the scene and what inspector says seems abnormal. When I print out the objects' positions through a script, the coordinates are different and normal. I have no idea why this would happen.

I tried a simple experiment to see if this same behavior is apparent in other situations. It behaved normally (unlike the situation I am trying to resolve).

Is there any explanation for this?

avatar image
0

Answer by kubajs · Mar 10, 2017 at 07:03 AM

Have you found the solution? I see the same problem now. I don't change any player rotation manualy. Just jump on rotated platform (parent player to platform) and when jumping out (unparenting), the global player's rotation is changed.

I can probably solve it using some rotation offset but I don't understand why this happens.

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 kubajs · Mar 07, 2017 at 12:00 AM 0
Share

Problem solved. Eric5h5 was right! Thanks Eric!

I retested the scenario with "clean" objects and couldn't re-create the issue. Then I immediately went and switched off suspected scripts. I realized Player Smooth $$anonymous$$ouse Look script (the only script I haven't written myself) was causing the issue. Once I disabled the script, everything works great! This is not the first problem this script caused. First one was terrible rigidbody flying like crazy under some circumstances. Now I start to understand the reason why ;).

The lesson is: 1. Don't depend on foreign scripts or you'll spend much more time with solving problems than to write your own script. 2. When having strange issue, try to recreate it with another (clean) objects and don't spend 2 hours on that like me...

Thanks guys.

avatar image
0

Answer by Ceachi · Mar 21, 2017 at 10:07 AM

Hello, here is an eazy way: 1) When you want to change the pozition of the object while you are in parent use for example transform.localPosition. Let say cube.transform.localPosition = new Vector3(-distance, 0, 0); Now when you unparent with cube.transform.parent=null , the global pozition will not be changed.

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
avatar image
0

Answer by Cambesa · Jul 28, 2021 at 05:30 PM

In my case it was displaced because of some hidden variables in IK. Visually it was correct, values in inspector were correct. But internally some values were displaced because of a new parenting structure and animations that were built without that parent. My solution was making a seperate object instead of using the original object.

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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Make a simple tree 1 Answer

add relative force to child after detach.children 1 Answer

How to control parenting in multiple functions 1 Answer

Lerp a Child but 'keep up locally' with Parent 0 Answers

Scale a prefab without reference to parent 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