Controlling Angle of Movement When Holding 2 Arrow Keys - 2d Top Down
Hey all,
I'm trying to figure out how to adjust the angle movement when hitting two arrow keys on they keyboard. Right now when I hit either the up+right key or the down+left key the character moves along the red arrow, but would like to adjust it to move along the green arrow's path instead.
Here's the code I'm working with currently.
public class PlayerMovement : MonoBehaviour { public float speed; void Update () { if(Input.GetKey(KeyCode.RightArrow)) { transform.Translate (Vector2.right * speed); } if(Input.GetKey(KeyCode.LeftArrow)) { transform.Translate (Vector2.left * speed); } if(Input.GetKey(KeyCode.UpArrow)) { transform.Translate (Vector2.up * speed); } if(Input.GetKey(KeyCode.DownArrow)) { transform.Translate (Vector2.down * speed); } }
I'm pretty new to Unity, so any suggestions would be really appreciated, thanks!
Your answer
Follow this Question
Related Questions
Character Diagonal Movement Issue 3 Answers
How can I incorporate a Rotation Towards the Mouse Position into this Script? I Tried. 0 Answers
Character movement that ends with a specific facing 0 Answers
How do I rotate my object back to the world's up direction whilst keeping it's forward direction? 0 Answers
Virtual camera odd behaviour. Scriptting noob questions 0 Answers