Temp Id
Temp Id

Reputation: 387

Child Rotation in Unity3D

I was trying Unity3d when I had a strange problem:

I created an empty object A and its child object cube B.

Properties of A: Position-(0,0,0) Rotation-(0,0,0) Scale-(1,1,1)

Properties of B: Position-(0,2,0) Rotation-(0,0,0) Scale-(1,4,1)

I have made a C# script having a Transform object. It rotates using transform.localrotation only along z-axis. The problem is that if in Unity Editor I attach A as transform, B seems to rotate along one of its corners (I don't know how was that corner chosen) and if I attach B, it seems to rotate along its centre. I don't understand why this is happening? I know that in localrotation, parent rotation is taken into account. But initial rotation of A and B are both zero, so it should not matter.

Upvotes: 1

Views: 5443

Answers (1)

jonas
jonas

Reputation: 627

If you modify transform.localRotation directly, you are modifying a quaternion.

Usualy you should not modify quaternions directly:

Tthey (sic!) are based on complex numbers and are not easy to understand intuitively. You almost never access or modify individual Quaternion components (x,y,z,w); most often you would just take existing rotations (e.g. from the Transform) and use them to construct new rotations (e.g. to smoothly interpolate between two rotations).

taken from the unity3d doc: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.html

Try using transform.Rotate() or transform.localEulerAngles. That should fix your problem.

http://docs.unity3d.com/Documentation/ScriptReference/Transform-localEulerAngles.html http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html

Upvotes: 2

Related Questions