Object to rotate along another objects axis?

I am trying to make a game where the player can rotate the world around them in order to escape from a maze.

At the moment i have a cube parented to the first person controller, and on a button press the cube and the world rotate in unison.

Here is the key piece of code :

function RotateWorld() {
    switch(directionFacing) {
        case  "forward" :
            transform.Rotate(Vector3(cubeRotationX * (90 * Time.deltaTime),
                0, cubeRotationZ * (90 * Time.deltaTime)), Space.Self);
            world.transform.Rotate(Vector3(cubeRotationX * (90 * Time.deltaTime),
                0, cubeRotationZ * (90 * Time.deltaTime)), Space.Self);
            break;         
        case "right" :
            transform.Rotate(Vector3(cubeRotationX * (90 * Time.deltaTime),
                0, cubeRotationZ * (90 * Time.deltaTime)), Space.Self);
            world.transform.Rotate(Vector3(cubeRotationZ * (90 * Time.deltaTime),
                0, -cubeRotationX * (90 * Time.deltaTime)), Space.Self);
            break;
        case "left":
            transform.Rotate(Vector3(cubeRotationX * (90 * Time.deltaTime),
                0, cubeRotationZ * (90 * Time.deltaTime)), Space.Self);
            world.transform.Rotate(
                Vector3(-cubeRotationZ * (90 * Time.deltaTime), 0,
                    cubeRotationX * (90 * Time.deltaTime)), Space.Self);
            break;
        case "backward" : 
            transform.Rotate(Vector3(cubeRotationX * (90 * Time.deltaTime),
                0, cubeRotationZ * (90 * Time.deltaTime)), Space.Self);
            world.transform.Rotate(
                Vector3(-cubeRotationX * (90 * Time.deltaTime), 0,
                    -cubeRotationZ * (90 * Time.deltaTime)), Space.Self);
            break;
    }
}

so basically this code rotates the cube and the world based on which direction the player is facing.

is there any way i can get the world to always rotate using the player's axis? ie. so that it 'knows' which way is up?

1 Answer

1

To rotate something using a known axis you could simply use either Transform.RotateAround to rotate the transform directly or Quaternion.AngleAxis to get the rotation which you would then apply to something else.

Assuming you want to do something like rotate the cube in place about the player's up axis, you could do:

//it's so simple, it doesn't need to be a different function, does it?
var player : Transform = GameObject.FindWithTag("Player").transform;
transform.RotateAround(transform.position, player.up, 90 * Time.deltaTime);

or

//still pretty simple
var player : Transform = GameObject.FindWithTag("Player").transform;
var rotation : Quaternion = Quaternion.AngleAxis(90 * Time.deltaTime, player.up);
transform.rotation *= rotation;

If you wanted to rotate the cube about the player's position as well as their axis, RotateAround would be easiest, because otherwise you would have to rotate the position as well

//Add this to the Quaternion stuff to turn it into a RotateAround
//around the player's position. 2 more lines = simple too.
var delta : Vector3 = transform.position - player.position;
transform.position = player.position + rotation * delta;

Side note:

Why are you rotating the world when you can just rotate the character/change the forces acting upon them (like changing Physics.gravity or the code where you apply your own movement to use the down relative to some pivot indicating your world forces (could be the character) when adding your own gravity)?

Yeah ive been having alot of problems with the player moving through meshes and then falling through the world so i might try rotating the character instead. thanks alot for the help!

sorry to bring this thread back from the dead, but ive decided to continue with this project after a bit of break. im now trying to do what you suggested, to rotate the player and the forces acting upon them. the problem is rotating the player. mouselook prevents external code from rotating the player, and i havent yet been able to find a way to smoothly override this. any suggestions? (ie code?) cheers.

Why did u use player.up? what does it do??

On the other computer, have you installed the same version of the Unity Editor? Just recently I found that builds are dependent on certain .dll's that are acquired after installing Unity. I can't imagine how this issue was missed... unless it's specific to the case of our software. I've experienced this issue with both Unity 4 and 5.