Rotating a child around his local Z axis relative to his parent

Hello, seems like a dumb question to ask but yet I can’t manage to make it work.

I have a gameobject Parent and a gameobject Child, both with a starting rotation of (0,0,0). When I press L, I want Child to have a rotation around his (Parent’s origin - 0.25f * Parent.transform.up) and around his local Z axis of an angle of 60°.

But if Parent rotates, I want Child to follow its Parent’s rotation.

So that if Parent has no rotation, Child will appear rotated 60° around his local Z, but if Parent is rotated 180° around his own local Y, Child appears rotated 60° around -Z, and so on.

I thought it would have been simple but seems like I can’t make it work.

Here’s what I did :

Quaternion targetRotation = Quaternion.AngleAxis(60, transform.forward); // transform is the parent’s transform, child is the child’s transform
    child.rotation = Quaternion.RotateTowards(child.rotation, targetRotation, Mathf.Infinity);

Thanks for the help !

Well, don’t use world space rotation then ^^. You currently use child.rotation and transform.forward which both “calculate” the world space orientation and world space forward axis. What you want to do instead is using Vector3.forward for the direction (or use Quaternion.Euler) and use child-localRotation.

Quaternion targetRotation = Quaternion.Euler(0,0,60);
child.localRotation = Quaternion.RotateTowards(child.localRotation, targetRotation, Mathf.Infinity);

ps: The RotateTowards line is currently completely unnecessary. It does the same as

child.localRotation = targetRotation;

since your max delta angle is infinity. So no matter what the new angle is, it would end up at that rotation instantly.

Hey Bunny83, thanks for the answer ! I’ll try that out when I get home ^^
But it seems you don’t control around which point you make the rotation. The “Parent’s origin - 0.25f * Parent.transform.up” part.

Where would that be done ?

Thanks :slight_smile:

Don’t ever try to simultaneously do a translation and rotation on the same object. You haven’t said what this actually was about, but if you want an object to “orbit” a certain point, just create another intermediate gameobject at that pivot point inside the parent by moving it to -0.25 on the local y axis. Make the actual object a child of that intermediate object and translate it upwards by 0.25. That way the actual object is back at (0,0,0) inside the outer parent. Now you can simply rotate the intermediate object that sits at -0.25 the way I explained. That way the object will “orbit” around that pivot

Oh clever ! I’ll try it out.
Thanks for the help !

Have a good day :slight_smile: