Search Unity

Need help! It should be simple!

Discussion in 'Scripting' started by freezingwreck, Sep 13, 2013.

  1. freezingwreck

    freezingwreck

    Joined:
    Mar 24, 2013
    Posts:
    21
    I want to make the strafing variable to be true when I press p and then i want the strafing variable to become false when I let go of p.

    Right now strafing becomes true when I press p but never becomes false.


    Code (csharp):
    1.  
    2. #pragma strict
    3. internal var animator : Animator;
    4. var strafing = false;
    5.  
    6.  
    7.  
    8. function FixedUpdate() {
    9.  
    10.  
    11.    
    12. if(Input.GetKeyDown("p"))
    13.     {
    14.         strafing = true;
    15.         animator.SetBool("strafe2", strafing);
    16.            
    17.     }
    18.     //the code below doesnt actually make strafing false!  
    19.     else if(Input.GetKeyUp("p") == true)
    20.     {
    21.     strafing = false;
    22.     }
    23.    
    24.    
    25. }
    26.  
    27.  
    28.  
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Try GetKey instead of GetKeyDown. Try it making the else if just an else.
     
    Last edited: Sep 13, 2013
  3. jvil

    jvil

    Joined:
    Jul 13, 2012
    Posts:
    263
    Why are you using FixedUpdate() to control the player movement? You should put this code into Update().

    FixedUpdate() should be used when dealing with physics like Rigidbody.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Also any *Down or *Up events will not work properly in FixedUpdate.

    --Eric
     
  5. freezingwreck

    freezingwreck

    Joined:
    Mar 24, 2013
    Posts:
    21
    I just did that and now it works! Thanks to all of you that are have tried to help though!!!

    I turned GetKeyDown to GetKey and voila !
     
    Last edited: Sep 13, 2013
  6. freezingwreck

    freezingwreck

    Joined:
    Mar 24, 2013
    Posts:
    21
    you guys are great!