Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

key code movement. Stuck on if statements.

Discussion in 'Scripting' started by Blaklights, Apr 2, 2018.

  1. Blaklights

    Blaklights

    Joined:
    Mar 13, 2018
    Posts:
    12
    Every time a key comes up i reduce speed to 0.
    How to to make so that if the A KEY is down, and D KEY comes up it won't set the speed to 0.
    I've tried everything I could google. I think I need help from someone that knows enough.
    Any help would be greatly appreciated.


    Code (CSharp):
    1.    
    2.  
    3.  if (Input.GetKeyDown(KeyCode.A))
    4.         {
    5.             speed = -speedX;
    6.         }
    7.         if (Input.GetKeyUp(KeyCode.A) && Input.GetKeyDown(KeyCode.D))
    8.         {
    9.  
    10.             speed = speedX;
    11.  }
    12.        
    13. else if (Input.GetKeyUp(KeyCode.A))
    14. {
    15.             speed = 0;
    16.            
    17.         }
    18.      
    19.        
    20.        
    21.         if (Input.GetKeyDown(KeyCode.D))
    22.         {
    23.             speed = speedX;
    24.         }
    25.         if (Input.GetKeyUp(KeyCode.D))
    26.         {
    27.            
    28.                 speed = 0;
    29.            
    30.         }
    31.        
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Use the InputManager, put it in a vector and apply that to the transform.

    Code (csharp):
    1. Vector3 input = Input.GetAxis("Horizontal");
    2. player.transform.position += input * Time.deltaTime;
     
  3. Blaklights

    Blaklights

    Joined:
    Mar 13, 2018
    Posts:
    12
    It's 2d game and I don't think that will work with the rest of my player manager script. I'll definitely give it a shot though. Thank you for such a quick reply.
     
  4. Blaklights

    Blaklights

    Joined:
    Mar 13, 2018
    Posts:
    12

    Didn't work. It won't allow the vector 3 statement.
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I think this is what you're after

    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.A))
    2. {
    3.     speed = -speedX;
    4. }
    5. if (Input.GetKeyDown(KeyCode.D))
    6. {
    7.     speed = speedX;
    8. }
    9.  
    10.  
    11. if (Input.GetKeyUp(KeyCode.A) && !Input.GetKey(KeyCode.D))
    12. {
    13.     speed = 0;
    14. }
    15. if (Input.GetKeyUp(KeyCode.D) && !Input.GetKey(KeyCode.A))
    16. {
    17.     speed = 0;
    18. }
     
    Blaklights likes this.
  6. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Alternative approach:
    Code (csharp):
    1.  
    2. int speed = 0;
    3. if (Input.GetKey(KeyCode.A))
    4.   speed -= speedX;
    5. if (Input.GetKey(KeyCode.D))
    6.   speed += speedX;
    7.  
    If both A and D are held down, they cancel each other out, and speed is back to 0. This is actually the logic behind Input.GetAxis, so you may as well use that instead.

    Note: It returns a float. You can cast it to int if needed.
     
    johne5 likes this.
  7. Blaklights

    Blaklights

    Joined:
    Mar 13, 2018
    Posts:
    12
    Bro, you're my f####### hero!
     
  8. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Wow, that's nice to hear! thanks.
     
    Blaklights likes this.
  9. Blaklights

    Blaklights

    Joined:
    Mar 13, 2018
    Posts:
    12
    I'm so serious. Thank you so very much. I've been struggling for a week and it was that simple. You did a good thing, and I GREATLY appreciate you taking your time to help me.