bausa
bausa

Reputation: 111

Rotation issue in Unity 3D

I have two square empty GameObjects. Both have a cube Object appended as a child. The top empty GameObject is also appended as a child two the lower one. How can I rotate the top empty GameObject, so the inner cube object is still on the other cube object (Right picture)?

When I use something like transform.localRotation, transform.localEulerAngles or transform.Rotate, the top object will always rotate like on the left picture.

Rotation of top empty GameObject

Upvotes: 1

Views: 1997

Answers (2)

user2635745
user2635745

Reputation:

Model your cubes in a 3d program like 3ds max or blender and change the pivot to the bottom of the cube where you want to rotate, its easier than to script to change position while rotating.

Upvotes: 0

Kay
Kay

Reputation: 13146

This kind of rotation implies a change of the object's position as well. I see two ways to solve this:

  1. Use Transform.RotateAround and specify the intersection of both, here the blue Z axis with some offset. Maybe its easier to use another empty object to get the coordinates.
  2. A hinge joint. This lets you coinfigure the behaviour perfectly but it works with non-kinmatic objects only. So you have to apply forces as the top object would be under control of the physics engine. Depends on the remaining part of the game if this is what you want.
  3. [Update]
    If you position the empty GameObject at the right place i.e. the intersection, and move the cube child object by half of its side length, you can get the desired rotation when rotating the cube instead of the parent. If you need a parent as independent object, you might introduce another empty, something like:
    • /RotatingAchorEmpty (placed at intersection)
    • /RotatingAchorEmpty/TopEmpty (position = distance between center and intersection line)
    • /RotatingAchorEmpty/TopEmpty/TopCube (cube itself)

Upvotes: 1

Related Questions