6

I've two gameObjects, sphereOne and sphereTwo as the children of an empty gameObject both.

I've this C# code attached to a sphereTwo

private void Update() =>
    transform.RotateAround(sphereOne.transform.position, 
        new Vector3(0, 1, 0), 100 * Time.deltaTime);

This lets sphereTwo rotate around sphereOne.

When I rotate the parent gameObject both, it rotates only on that specific plane.

enter image description here

How do I dynamically update the transform position of the rotating sphere so that it lies on the same plane as the parent Object's rotation?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
ptamzz
  • 9,235
  • 31
  • 91
  • 147
  • Set it's position vector to that of it's parent in regards to the correct plane? – S.Ahl Aug 02 '16 at 15:46
  • the secret here is you have AN EMPTY GAME OBJECT (often called a "marker") which "holds" the things. – Fattie Aug 02 '16 at 18:46
  • Possible duplicate of [Difficulty with rotating child around parent axes](http://stackoverflow.com/questions/37753519/difficulty-with-rotating-child-around-parent-axes) – Fattie Aug 02 '16 at 18:46

1 Answers1

12

The second parameter in Transform.RotateAround() is the axis which determines the orientation of the plane on which sphereTwo rotates around sphereOne. Right now, you have this set to a static value of new Vector3(0, 1, 0), basically the world's up vector.

To have the axis instead be based on the orientation of sphereOne, use its Transform.up vector instead - this will change based on the world-space rotation of sphereOne's transform:

private void Update() 
{
    transform.RotateAround(sphereOne.transform.position, sphereOne.transform.up, 100*Time.deltaTime);
}

(You could probably alternatively use both.transform.up, depending on the situation.)

Hope this helps! Let me know if you have any questions.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Serlite
  • 12,130
  • 5
  • 38
  • 49
  • that's all true but the OP needs to learn how to stack up transforms so as to achieve stuff like this! – Fattie Aug 02 '16 at 18:46