Problems with transforms - Child compensating parent transform [FIXED]

Hi all,

I am working with GameObjects, which are created by script and which are hierachically attach to each other like this:

var go = new GameObject("Parent");
go.transform.parent = parentObject.transform;

But I run into one thing, I don’t understand. When I

  1. create a parent
  2. rotate it by 45 degrees
  3. create a child
  4. attach a child to parent
    the child somehow compensates the parent transform e.g. having a y-rotation of 315 after this procedure. Even setting the rotation to Quaternion.Identity won’t fix this problem!
    If I do step 2 as the last step, the problem does not occur. Unfortunately, that’s not always possible for me.

Why does Unity3D this and how can I avoid it?

If necessary I can provide sample code for this issue.

Regards,
Jonas

Unity does this because it is the expected outcome. Parenting something doesn’t immediately mean that it should orient to it.

Your solution was close, but you actually want to set localRotation to Quaternion.identity instead of rotation.

Hey, thanks for the quick answer! Using localRotation does the trick. Usually I would exspect it to stack/concatenate the transforms, as I am used from other engines.
But hey, it works now!