I'm making a space invaders style game and I would like to move my ship in a diagonal direction. Currently, I have a series of if statements in the Update function Spoiler: Code Code (CSharp): if (Input.GetKey (KeyCode.LeftArrow)) { print ("left"); MoveLeft (); } else if (Input.GetKey (KeyCode.RightArrow)) { print ("right"); MoveRight (); } else if (Input.GetKey (KeyCode.UpArrow)) { print ("up"); MoveForward (); } else if (Input.GetKey (KeyCode.DownArrow)) { print ("down"); MoveBack (); that allow me to move left, right, up and down using Vector3.left/right/up/down. Spoiler: More Code Code (CSharp): void MoveLeft(){ shipPos += Vector3.left * speed * Time.deltaTime; } void MoveRight(){ shipPos += Vector3.right * speed * Time.deltaTime; } void MoveForward(){ shipPos += Vector3.up * speed * Time.deltaTime; } void MoveBack(){ shipPos += Vector3.down * speed * Time.deltaTime; } How would I go about making diagonal movement? I already tried adding Spoiler: These lines of code to Update() Code (CSharp): else if (Input.GetKey (KeyCode.RightArrow) && Input.GetKey (KeyCode.UpArrow)) { MoveUR(); } else if (Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.UpArrow)) { MoveUL(); } else if (Input.GetKey (KeyCode.RightArrow) && Input.GetKey (KeyCode.DownArrow)) { MoveDR(); } else if (Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.DownArrow)) { MoveDL(); } Code (CSharp): void MoveUR(){ shipPos += (Vector3.up + Vector3.right) * speed * Time.deltaTime; } void MoveUL(){ shipPos += (Vector3.up + Vector3.left) * speed * Time.deltaTime; } void MoveDR(){ shipPos += (Vector3.down + Vector3.right) * speed * Time.deltaTime; } void MoveDL(){ shipPos += (Vector3.down + Vector3.left) * speed * Time.deltaTime; } but my tests so far seem to indicate that Input.GetKey(KeyCode.key) can't handle more than one thing being input at the same time and my searches haven't brought anything useful up.
Code (CSharp): if (Input.GetKey (KeyCode.LeftArrow)) { print("left"); MoveLeft(); } else if (Input.GetKey (KeyCode.RightArrow)) { print("right"); MoveRight(); } if (Input.GetKey (KeyCode.UpArrow)) { print("up"); MoveForward(); } else if (Input.GetKey (KeyCode.DownArrow)) { print("down"); MoveBack(); }
You may want to investigate normalising so that the diagonal movement isn't faster than the up/down/left/right
That's a good point. I'll look into that, but it looks like I might have to restructure the way I coded the movement so I'm only adding one vector to the transform.position