How to move my 2D player up or down on the vertical axes depending on if it is facing up or down?
Below is my code that currently rotates the character:
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate (Vector3.back);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate (Vector3.forward);
}
Basically, I got the player moving up, down, left and right to start with.
Then I got the player to be able to rotate.
I then deleted the original movement part of my code, as I just want the player to be able to move up or down on the vertical axis, depending on whether it is facing the up or down direction.
Can anyone help me with this?
Cheers in advance dudes!
Answer by jimmiewalker653 · Nov 26, 2020 at 02:50 PM
Instead of using Vector3, use Vector2. Next, you'll want to specify if they press the UpArrow or DownArrow, like this...
void Update()
{
if (Input.GetKey(KeyCode.UpArrow))
{
Transform.Translate (0, speed,0);
}
}
If you are using a rigidbody2D, then you could do an AddForce such as...
void Update()
{
if (Input.GetKey(KeyCode.UpArrow))
{
rb2d.AddForce(transform.up * speed);
}
}
Your answer
Follow this Question
Related Questions
Player facing mouse with orthographic isometric camera 0 Answers
How do I create a 2D movement script that will cause prefabs to drift around the screen? 0 Answers
Unexpected symbol '=' parser error and Unexpected symbol '(' error 1 Answer
8-way 2D top down movement (diagonal) idle animation issues 0 Answers