# Rotate cube according to position of another object?

I have a level as shown below.

I want to press any arrow key and the blue cube will ‘rotate’ or move position to same position but on a different face of the white cube.

I want this to work so whatever arrow key you press it will move it to the same relative position but on a different face of the white cube. I can’t figure out how to manage this any help is much appreciated.

You can parent an object to another object, and it will move along with it. So, when you press a key, set the blueCube to be a child of another transform of your choosing. Then perform the animation on the new-parent. If you keep world position when stetting the parent, the object will stay put, even though it “follows” a different object.

``````// ...pick a newParentTransform
blueCube.transform.SetParent(newParentTransform, worldPositionStays=true);
// ...play an animation that does the rotation
``````

Another possible approach is to set the rotation directly with Transform.RotateAround

Hey, you were able to figure out how to do this?

This will rotate the entire cube at the same time and will not work if you wanted a rubics cube(1x1x1 cube parented under a 3x3x3 cube)

``````private int RotationAmount = 90;
private int xRotation; // Top and Bottom side exclusive
private int zRotation; // Left and Right side exclusive
[SerializeField] private GameObject cube;

void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{ xRotation += 90; }

if (Input.GetKeyDown(KeyCode.DownArrow))
{ xRotation -= 90; }

if (Input.GetKeyDown(KeyCode.LeftArrow))
{ zRotation += 90; }

if (Input.GetKeyDown(KeyCode.RightArrow))
{ zRotation -= 90; }

cube.transform.eulerAngles = new Vector3(xRotation, 0, zRotation);
}
``````

This will not work as a rubics cube because then you need different input for all axis of movement (3) for every column/line (3 with this cube) aka 9.