Search Unity

Rotating One Object Relative to Another

Discussion in 'Scripting' started by marty, Mar 30, 2007.

  1. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    How do I rotate a transform relative to another transform?

    There's relativeTo = Space.Self and relativeTo = Space.World, but nothing allowing me to execute the rotation relative to another object/transform.

    But I know there's got to be a way to do it. ;-)
     
  2. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    maybe something along the lines of using

    transform.rotation *= target.rotation - transform.rotation

    somewhere in your rotation. maybe. not sure what you're going for and don't know if there's a built in.
     
  3. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    For example, if I had an "earth" object and a "moon" object, neither of which are at World (0,0,0), and I wanted to rotate the moon about the earth, how would I do that?
     
  4. Willem

    Willem

    Joined:
    Mar 9, 2007
    Posts:
    184
    If you drag your moon object to be a child of your earth object in the hierarchy view, it will automatically rotate around it's parent.
     
  5. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Good point.

    But I don't want the earth and moon to be grouped and I want the earth to not rotate. Just the moon.
     
  6. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
  7. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Hey! There you go!

    Thanks, Matthew and all!