Rotate an object such that it is theta degrees relative to another object

I have two cubes where one is static and another moves freely.

The point of the program is to move a cube by a random increment (for the X and Z coordinates) till they reach the ideal distance and ideal angle (10 units and 45 degrees) and they’re supposed to move and rotate only along the XZ Axis (The camera is orthographic pointing downwards along the Y axis).

Right now I have the moving part of it figured out and working perfectly.

The only thing I have a problem with is the rotating part.

I tried moving the cube relatively to the static cube but when I do that, the moving cube pivots around the static cube instead of rotating in its place to an angle such that it makes a 45 degree angle with the static cube. I did the above using: transform.RotateAround(staticCube.transform.position, Vector3.up, 45); and it does not work.

Any ideas on how I can do this?

Hey there,

just a wild guess but this should solve your issue:

transform.RotateAround(transform.position, Vector3.up, 45);

RotateArounds first argument is the position that you will use as pivot.
So easier would probably be to use transform.Rotate() here since it defaults to transform.position as pivot point. (Not tested but i think it’s this way. Let me know if that was wrong :))