2
\$\begingroup\$

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));
        rigid.AddRelativeForce(Vector2.left);
    }

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

    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.AddRelativeForce(new Vector2(0, jumpPower));
        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 :> )

Image!


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

\$\endgroup\$
  • \$\begingroup\$ Sounds like you want the input to be in world space, rather than object/local space. Therefore use AddForce() rather than AddRelativeForce(). \$\endgroup\$ – Droppy Aug 22 '16 at 14:58
0
\$\begingroup\$

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))
{
    rigid.AddRelativeForce(Vector2.left.Rotate(worldRotation));
}

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

\$\endgroup\$
  • \$\begingroup\$ '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 :< \$\endgroup\$ – BiiX Feb 11 '16 at 5:33
  • \$\begingroup\$ 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. \$\endgroup\$ – RobStone Feb 11 '16 at 5:39
  • \$\begingroup\$ @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.) \$\endgroup\$ – BiiX Feb 11 '16 at 6:10
  • \$\begingroup\$ @RobStone I guess rotating the camera could work too. But I would still have the same problems. \$\endgroup\$ – BiiX Feb 11 '16 at 6:22
  • \$\begingroup\$ @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". \$\endgroup\$ – wondra Feb 11 '16 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.