Search Unity

Moving diagonally in 2D

Discussion in 'Scripting' started by VhalirGames, Aug 4, 2015.

  1. VhalirGames

    VhalirGames

    Joined:
    Aug 4, 2015
    Posts:
    19
    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 (CSharp):
    1. if (Input.GetKey (KeyCode.LeftArrow)) {
    2.             print ("left");
    3.             MoveLeft ();
    4.         } else if (Input.GetKey (KeyCode.RightArrow)) {
    5.             print ("right");
    6.             MoveRight ();
    7.         } else if (Input.GetKey (KeyCode.UpArrow)) {
    8.             print ("up");
    9.             MoveForward ();
    10.         } else if (Input.GetKey (KeyCode.DownArrow)) {
    11.             print ("down");
    12.             MoveBack ();
    that allow me to move left, right, up and down using Vector3.left/right/up/down.
    Code (CSharp):
    1. void MoveLeft(){
    2.         shipPos += Vector3.left * speed * Time.deltaTime;
    3.     }
    4.  
    5.     void MoveRight(){
    6.         shipPos += Vector3.right * speed * Time.deltaTime;
    7.     }
    8.  
    9.     void MoveForward(){
    10.         shipPos += Vector3.up * speed * Time.deltaTime;
    11.     }
    12.  
    13.     void MoveBack(){
    14.         shipPos += Vector3.down * speed * Time.deltaTime;
    15.     }
    How would I go about making diagonal movement?
    I already tried adding
    Code (CSharp):
    1. else if (Input.GetKey (KeyCode.RightArrow) && Input.GetKey (KeyCode.UpArrow)) {
    2.             MoveUR();
    3.         } else if (Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.UpArrow)) {
    4.             MoveUL();
    5.         } else if (Input.GetKey (KeyCode.RightArrow) && Input.GetKey (KeyCode.DownArrow)) {
    6.             MoveDR();
    7.         } else if (Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.DownArrow)) {
    8.             MoveDL();
    9.         }
    Code (CSharp):
    1. void MoveUR(){
    2.         shipPos += (Vector3.up + Vector3.right) * speed * Time.deltaTime;
    3.     }
    4.  
    5.     void MoveUL(){
    6.         shipPos += (Vector3.up + Vector3.left) * speed * Time.deltaTime;
    7.     }
    8.  
    9.     void MoveDR(){
    10.         shipPos += (Vector3.down + Vector3.right) * speed * Time.deltaTime;
    11.     }
    12.  
    13.     void MoveDL(){
    14.         shipPos += (Vector3.down + Vector3.left) * speed * Time.deltaTime;
    15.     }
    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.
     
  2. oOHicksyOo

    oOHicksyOo

    Joined:
    Feb 22, 2015
    Posts:
    17
    Code (CSharp):
    1. if (Input.GetKey (KeyCode.LeftArrow))
    2. {
    3.     print("left");
    4.     MoveLeft();
    5. }
    6. else if (Input.GetKey (KeyCode.RightArrow))
    7. {
    8.     print("right");
    9.     MoveRight();
    10. }
    11.  
    12. if (Input.GetKey (KeyCode.UpArrow))
    13. {
    14.     print("up");
    15.     MoveForward();
    16. }
    17. else if (Input.GetKey (KeyCode.DownArrow))
    18. {
    19.         print("down");
    20.         MoveBack();
    21. }
     
  3. VhalirGames

    VhalirGames

    Joined:
    Aug 4, 2015
    Posts:
    19
    Thanks for the help!
     
  4. oOHicksyOo

    oOHicksyOo

    Joined:
    Feb 22, 2015
    Posts:
    17
    No problem , if you need anything else just shoot me a message
     
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You may want to investigate normalising so that the diagonal movement isn't faster than the up/down/left/right
     
  6. VhalirGames

    VhalirGames

    Joined:
    Aug 4, 2015
    Posts:
    19
    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