# Set input relative to map rotation?

I'm trying to figure out how I can make the input which looks like this :

    // Checks if player is pressing movement keys, if is applies force to specified direction
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
//rigid.AddForce(new Vector2(-speed * Time.deltaTime, 0));
}

if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
//rigid.AddForce(new Vector2(speed * Time.deltaTime, 0));
}

if (Input.GetKey(KeyCode.UpArrow) && groundCheck.isGrounded == false || Input.GetKey(KeyCode.W) && groundCheck.isGrounded == false || Input.GetKey(KeyCode.Space) && groundCheck.isGrounded == false)
{
groundCheck.isGrounded = false;
rigid.velocity = new Vector2(rigid.velocity.x, jumpPower);
}


Always move according to the maps rotation.

So, even if the map were to rotate the player doesn't get inverted inputs (player is children of map, so it rotates too) :

Right now A is always to left, according to the players position, but if the player would rotate 90 degrees the input would be wrong, Left would be up/down, but I want it to still be left.

Here's a picture example : (Sorry for the horrible painting, I blame mouse :> )

I do want the physics in the world to not change when I rotate the world.

So if the player is rotated 90 degrees it should fall to the side instead of down. :D

• Sounds like you want the input to be in world space, rather than object/local space. Therefore use AddForce() rather than AddRelativeForce(). Aug 22, 2016 at 14:58

The key is knowing current world rotation, you need to maintain it as you rotate e.g.:

double worldRotation;

void rotateWorldLeft()
{
worldRotation = (worldRotation + 90) % 360;
}


then, when moving the character, just Rotate the Vector2s along e.g.:

if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
}


Alternatively, if you have only few and known in advance possible rotations, you could use lookup table coupled with enum and modulus:

Enum Direction { Up = 0, Left, Bottom, Right }

Vector2[] Directions = { Vector2.up, Vector2.left, -Vector2.up, Vector2.right };

Direction worldOrientation; /*again keep it up-to date*/
...
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
rigid.AddRelativeForce(Directions[((int)Direction.Left + (int)worldOrientation) % Directions.Length]);
}


note: the first solution should be preferred, casting enum to int is not the best practice - I am mentioning this only should you prefer avoid vector math completely

• 'Vector2' does not contain a definition for 'Rotate' and no extension method 'Rotate' accepting a first argument of type 'Vector2' could be Uh, adding Rotate after Vector2.left doesn't work :<
– BiiX
Feb 11, 2016 at 5:33
• To be clear, you are rotating the camera and not the world right? Adjust your input using the Y axis rotation as input angle offset. Feb 11, 2016 at 5:39
• @RobStone No, I'm rotating the world so I still have the same physics as I had before the world was rotated. (also, the player is a part of the world so it gets rotated as well.)
– BiiX
Feb 11, 2016 at 6:10
• @RobStone I guess rotating the camera could work too. But I would still have the same problems.
– BiiX
Feb 11, 2016 at 6:22
• @BiiX ofcourse there is no .Rotate in Unity, thats why I linked(in case you couldnt find it on google) you the implementation of extension method. Just follow the link under "rotate". Feb 11, 2016 at 9:44