Search Unity

Animation help! How can I start a transition with a key press?

Discussion in 'Animation' started by Blue Bean Apps, Jan 18, 2015.

  1. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Hey, everyone. I am working on a Super Mario Bros. clone that I am making to learn more about Unity (I'm a noob :p). When Mario jumps, there is the jump audio clip that plays. In my script, I have it set to jump when any of the following keys: w, space, up arrow or the 8 on the keypad are pressed, but this event falls under the FixedUpdate function. When I tried to put audio.Play in there, it played, but since it is in the update function, it plays it every frame, but I only want it to play once each time you press any of the jump keys.

    I am wondering if there is a way to do this through an animation. I could call the specific animation state that will play the jump audio clip from within the script when the required key is pressed. However, I don't know if, by calling this animation state from within the update function, it will play the animation every frame.

    Think of it like how the new UI in 4.6 works with an event system. You have an OnClick function that performs a function from within the attached script. It would be nice if they had this sort of system for more things than just the UI, but I don't think they do. Correct me if there is something like this!

    Is there a way to not get an event that is in the Update/FixedUpdate function to NOT be called every frame?

    Thanks,

    - Chris
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Hey Chris

    Disclaimer - I'm an animator and know nothing about code!

    I seem to recall a similar problem being asked about on here and if I remember the solution was to call the 'problem child' - in this case the audio clip in late update?!? maybe. I'm probably totally wrong - feel free to laugh out loud if I'm talking out the side of my head.
    Just thought I'd drop a hint I seem to remember being related to the issue you are experiencing.

    If it happens that I'm miraculously correct about this the world may be coming to an end because like I stated before me-no-code. :)

    Hope you figure it out.
     
  3. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    @theANMATOR2b I appreciate the suggestion, but I am new to code as well, so you can call me 'theCODER2b'. :p So I have no idea what you mean by the 'problem child'. If you are suggesting to play the audio clip from within the LateUpdate function, rather than the Update function, then I think this might work... According to the script reference, LateUpdate happens after all other Update functions have been executed, so in my case, LateUpdate would execute audio.Play after the Update function has acknowledged the key press to jump.

    I will give this a try and I will get back to you.

    Thanks!
     
  4. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    You got it - that's what I was referring to, though not sure if it's the correct solution you need.