Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Benji23245, Jul 3, 2023.

  1. Benji23245

    Benji23245

    Joined:
    May 7, 2020
    Posts:
    127
    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 :

    Code (CSharp):
    1. Quaternion targetRotation = Quaternion.AngleAxis(60, transform.forward); // transform is the parent’s transform, child is the child’s transform
    2.     child.rotation = Quaternion.RotateTowards(child.rotation, targetRotation, Mathf.Infinity);
    3.  
    Thanks for the help !
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    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.

    Code (CSharp):
    1. Quaternion targetRotation = Quaternion.Euler(0,0,60);
    2. child.localRotation = Quaternion.RotateTowards(child.localRotation, targetRotation, Mathf.Infinity);
    ps: The RotateTowards line is currently completely unnecessary. It does the same as

    Code (CSharp):
    1. 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.
     
  3. Benji23245

    Benji23245

    Joined:
    May 7, 2020
    Posts:
    127
    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 :)
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    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
     
  5. Benji23245

    Benji23245

    Joined:
    May 7, 2020
    Posts:
    127
    Oh clever ! I’ll try it out.
    Thanks for the help !

    Have a good day :)