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. Dismiss Notice

How to only progress animation when a button is depressed?

Discussion in 'Animation' started by garymon, Jan 22, 2015.

  1. garymon

    garymon

    Joined:
    Dec 16, 2014
    Posts:
    2
    Just getting started with Unity. I have a product model in a scene. There are animations that transition the model from one of its physical states to another (imagine a recliner chair going from upright to laid out for example). Different keyboard keys trigger the various animations. That was all pretty straightforward for me. After looking at the tutorials here.

    Now the designers that will be using this tool for design discussions would like the animation to only progress while ta key is held down, to pause if they lift up their finger (to discuss or point out a detail) and then resume playing if they press the key again.

    I thought I might be able to call Play on keydown and Stop on keyup but Stop rewinds the animation according to the docs. There is no pause that I see.

    It seems the animation control does not have the granularity I need. Is there a way to manually move through the frames?

    Thoughts? Is this fundamentally counter to how Unity works or am I just missing something obvious?

    Thank you!
    Gary
     
  2. Octo-Games

    Octo-Games

    Joined:
    Jan 6, 2013
    Posts:
    45
    There isn't a preset function for this but you can put the current frame of animation in a variable when the key is up and then restart from that frame when the key is down again

    although you have to check the animation's time value.

    Then to convert from time to frame you multiply by your animation's framerate. Just note that because of variable framerates, you're not guaranteed to be exactly on a frame so use greater than / less than to check if you've passed a given frame or are between the two frames you're looking for.

    After that you will have to restart from the frame on the key down.

    you can have an example here i think : http://answers.unity3d.com/questions/317651/play-animation-from-frame-x-to-frame-y.html

    and to stop the animation use the function animation.stop


    i think it should work this way
     
    theANMATOR2b likes this.
  3. garymon

    garymon

    Joined:
    Dec 16, 2014
    Posts:
    2
    Thank you blade00x! I really appreciate the help. I will check-out the example project.

     
  4. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790

    That's an awesome explanation @blad00x. Not to hijack because it's relevant - is this the standard process for most games when the player clicks the pause button - or menu/options button - presses the start button on a controller which is usually pause?
     
  5. Octo-Games

    Octo-Games

    Joined:
    Jan 6, 2013
    Posts:
    45
    Hi @theANMATOR2b i don't thing that this is the most used way to pause a game because it will use a lot of variable and ressource just to save the animation position etc.. etc...

    On unity 3D you can simply "freeze the time" wich freeze the game itslef by changing timeScale to 0 (for freeze) and to 1 (to continue the execution of the game).

    Of course you will have to create a boolean to check either if the game is paused or not, in my prototype and game i used a function called Pause() and a variable IsPaused wich is a bool then i simply can change the situation of the game(freezed or not)

    it's way way easier than freeze every animation seen by the player ^^

    Hop i answered your questions
     
    theANMATOR2b likes this.