Search Unity

Script for animation pause

Discussion in 'Scripting' started by Silveralby, Jun 12, 2019.

  1. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi guys I have the following script to activate a keyboard animation. How could I modify the script so that when I press the key the animation starts while if I leave the key the animation pauses? Thank you
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Benna : MonoBehaviour {
    6.  
    7.     public Animator anim;
    8.  
    9.     // Use this for initialization
    10.     void Start() {
    11.         anim = GetComponent<Animator>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update() {
    16.         if (Input.GetKeyDown(KeyCode.V))
    17.         {
    18.             gameObject.GetComponent<Animator>().Rebind();
    19.             anim.Play("Benna");
    20.            
    21.         }
    22.     }
    23. }
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
  3. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi Lethn not really. In practice I have to move a part of a hydraulic machine and I would like to keep the "V" key pressed to start the animation while leaving the "V" key the animation pauses then resume if I press the button again.
     
    Last edited: Jun 12, 2019
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Animator have manual time mode. Start playing immediately and advance time by Time.deltaTime if key is down.
     
  5. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi palex-nx I'm not very familiar with scripts, could you put the Time.deltaTime in the script up? Thank you so much.
     
    Last edited: Jun 13, 2019