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 Code
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.More Code
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 These lines of code to Update()
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();
}
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.