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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Animation problem to do with the update or GetKey function? (C#) {SOLVED}

Discussion in 'Scripting' started by milliehashh, Feb 5, 2016.

  1. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    I'm having a problem to do with my animation restarting every time the 'W' key is pressed, i want the animation to play just once when i hold down the 'W' key yet its constantly restarting instead - is this because i have my code within an update function?

    here is my code btw
    Code (CSharp):
    1. if (Input.GetKey (KeyCode.W)) {
    2.                 Debug.Log ("jump should occur");
    3.                 playerAnimator.SetBool ("startjumping", true);
    if not then in there any way of my fixing this?

    thanks in advance
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    It depends on your state machine. Can you post a screen shot of your animator component showing states and transitions? If your transition that depends on 'startJumping' comes from the 'Any State' block, then I can see this happening.

    Also something to check: is there anything in your script that will reset the 'startJumping' bool to false? If there isn't, then your controller will keep it at true forever after pressing 'w' the first time. If you don't want to do this, you can use a trigger instead. Triggers will automatically reset when used as a transition condition.

    You might also want to double check your transition back to the previous state to make sure it only returns when you want it to, most likely when the character hits the ground again.
     
  3. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Are you starting it in an update method, if so call a function in start and have the function call itself, or see if the animation is playing.
     
  4. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    i am currently using an Any State block for start jumping, what do you recommend i do instead?

    i have a line in my script that sets it to false again but I'm not sure if thats another thing causing this problem
    Code (CSharp):
    1. if (Input.GetKey (KeyCode.W)) {
    2.                 Debug.Log ("jump should occur");
    3.                 playerAnimator.SetBool ("startjumping", true);
    4.             }
    5.  
    6.             else
    7.             playerAnimator.SetBool ("startjumping", false);
    8.         }
    i tried not to use triggers as this is actually for a side scrolling game I'm working on, this is the character animation :)
     
  5. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    hey, just thought i'd state that i think the problem is with the character controller i was using as an example, ive written out a different script for the controller and it all works perfectly fine now

    The script i was using was from here http://wiki.unity3d.com/index.php?title=FPSWalkerEnhanced

    thank you for all the help anyways, it seems my problem is fixed now :)
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    The problem is that you're using Input.GetKey, which returns true on every frame the button is held down.

    GetKeyDown only returns true on the frame the key is pressed, so that should fix things. Nice to know for future reference even if you worked around the problem in a different way this time :)
     
    milliehashh likes this.
  7. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    ahh thank you so much, i will definitely remember that for the future! :D