• 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 inigopanos · Oct 28, 2020 at 03:29 PM · rotationscaleparent-childparent transform

children doesn't take parent's rotation, position or scale.

So as the title says, I have some gameobjects I want to be children of other gameobjects. However, changing their transforms don't work. The only one that works is the localScale one, but it makes the child have a different size from the one they have when they are not a child.Their rotation is completely random at times, as the inspector says the child is at 0,0,0 when it visually changed by 90º on the X axis. Any help on how to make them take the parents position, rotation or scale is appreciated.

This here is my code.

  private void OnTriggerEnter(Collider col) 
     {
         if ((col.CompareTag("RopaClara") || col.CompareTag("RopaOscura")) && !Ocupado_1)
         {
             col.transform.SetParent(pos_1);
             col.transform.localPosition = new Vector3(0, 0, 0);
             col.transform.rotation = pos_1.rotation;
             //col.transform.localScale = new Vector3(5f, 5f, 5f);
             Debug.Log(col.transform.position);
             Ocupado_1 = true;
 
             if (!Ocupado_2)
             {
                 col.transform.SetParent(pos_2);
                 col.transform.localPosition = pos_2.transform.localPosition;
                 col.transform.localScale = new Vector3(2f, 2f, 2f);
                 Debug.Log(col.transform.position);
                 Ocupado_2 = true;
             }
Comment
Add comment · Show 1
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 cyoedhana · Oct 28, 2020 at 04:34 PM 0
Share

Can you try using col.gameObject.transform instead of col.transform? Just to make sure that you actually establish parent-child relationship between pos_1, or pos_1 and the GameObject that col is attached to.

1 Reply

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

Answer by Quelfth · Oct 28, 2020 at 05:00 PM

Transform.position and Transform.rotation are the absolute position and rotation of the transform. Transform.localPositon, Transform.localRotation and Transform.localScale are their position, rotation, and scale, relative to their parent. So, when you said col.transform.localPosition = pos_2.transform.localPosition, that's going to be setting the position of col relative to pos_2 to whatever pos_2's location is relative to it's parent. I can't imagine this is intended, since even assuming that pos_2 doesn't have a parent, and that it isn't rotated, col's position will end up being double pos_2's position is in world space.

If you want a transform to be at the same position as its parent, do

Transform.localPosition = Vector3.zero.

If you want it to be the same rotation as its parent, do

Transform.localRotation = Quaternion.identity.

And if you want it to be unrotated in world space, do

Transform.rotation = Quaternion.identity.

If any of that doesn't seem to be doing what I said it would, you should make sure that the parent object isn't rotated or scaled, and that none of its parents are rotated or scaled.

If any of the parents in the hierarchy have rigidbodies, it's possible they're being rotated without your knowledge, and you may want to find and check the restraint check boxes on the rigid body.

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 cyoedhana · Oct 28, 2020 at 07:31 PM 0
Share

Hi @Quelfth : whose transform is returned when calling Collider.transform? Is it the Collider object's own transform, or the transform of the GameObject instance that the Collider object is a part of?

avatar image inigopanos · Oct 29, 2020 at 12:28 PM 0
Share

Thank you very much for the answers! What I currently have is a box, with a gameobject child which also has all the pos_x as children, and each one of those pos_x needs to be able to have at least one more child, and these last children are the ones I need to set the rotation, position and scale.

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

187 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 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

Impossible to rotate child using parent rotation gizmo 1 Answer

Relative transforms and parenting 1 Answer

Translating measurement in rotation speed and game object size to non-technical staff 0 Answers

Object Creation 2 Answers

Global rotation on child object ? 2 Answers

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