• 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
1
Question by Urkins · Dec 29, 2017 at 03:59 PM · positionparentparentingchild objectlocalposition

Set child position and move parents with

Hello, I'm creating a game and I have a problem. I have a gameObject with a rigidbody and some script. This gameObject has 2 child and each child has a collider (trigger).
In a script I need to set the position and the rotation of the child and I want that the parent come with the child so the global structure stay the same.
I tried with a the localPosition but nothing work.
Do you get any idea?
Thanks

Comment
Add comment · Show 3
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 zero_null · Dec 29, 2017 at 04:32 PM 1
Share

why don't you position the parent ? Child will move wit it.

avatar image Urkins zero_null · Dec 29, 2017 at 04:36 PM 0
Share

Yes but I can't find how to do that. I have a bug I think on the local position.
When I try do : parent.position - (child.position - child.localPosition) I get 2.0154 but it should be. Any idea?

avatar image stratos_kakalis · Dec 29, 2017 at 06:36 PM 0
Share

Can you be a little more specific or post a script of what it is you are trying to achieve.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Exonto · Dec 29, 2017 at 06:44 PM

Moving or rotating a parent object and its children should be done by transforming the parent. All child objects will automatically transform with the parent. What this means is that you should consider trying to use the parent to move your game object rather than moving the children and then mathematically calculating where the parent should be. You can think of the children as being anchored to the parent, and anything the parent does, its children will do also. This does not work the other way around.

Also, make sure that you are changing the parent's rotation/position correctly in your scripts. Don't do this:

 var position = parentTransform.position; // This is a copy of the parent's position
 var otherPosition = new Vector(1.0f, 2.0f, 3.0f);
 position = position - otherPosition; // Will not change the transform of the parent object

In order to actually set the position, you must instead do:

 var otherPosition = new Vector(1.0f, 2.0f, 3.0f);
 // Will correctly change the transform
 parentTransform.position = parentTransform.position - otherPosition; 

Make sure you are setting the position or rotation directly from the object. Don't store it in a variable and expect it to actually transform the game object. Hope this helped.

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 NUKETheWay · Nov 20, 2021 at 01:19 PM 0
Share

Can't use this if we want to addForce or Velocity to child. Its like a loop question.

We need physics to work while moving.

But simply changing parent positioning to affects childs won't allow that.

Now if we addForces or velocity to Child, parent position will stay at origin and only child will move.

If we try to set parent's position = child position

Doing this will again affect child's further position.

With transform.position we can move all them together but without any physics happening on child.

If we move child with velocity or forces, it won't affect parent.

avatar image
0

Answer by JP_Ferreira · Dec 29, 2017 at 06:45 PM

I don't know if i'm getting it right, but i think you can invert the parent order.

So it would be something like: When you are moving the parent from the child. transform.SetParent(childTransform, true);

When you want to reverse to originalState: childTransform.SetParent(transform.Transform,true);

Didn't test it.

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

89 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

Related Questions

Adjusting localPosition 1 Answer

Updating localPosition(?) in a scaled transform parent 1 Answer

Why is parenting not working? 0 Answers

Object position and its size 0 Answers

Scale gameobject around vector 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