- Home /
Animate only after certain time
Hello everyone,
I have a tank that always animates its "Scanning/waiting" animation when no key is pressed. I want it to play its scanning animation only after a certain amount of time has passed after having not pressed a key.
So how do I measure the time elapsed after having pressed a certain key?
I'm using JavaScript for this project by the way, but I can also use C#.
I know it may have something to do with Time.time, but I can't get my head around it.
Any help appreciated-Hyperion
Answer by bubzy · Aug 01, 2013 at 10:06 PM
 using UnityEngine;
 using System.Collections;
 
 public class time : MonoBehaviour {
     
     public bool idle = false;
     public float timeToIdle = 2.0f; //2 seconds
     float currentTime = 0f;
     
     // Use this for initialization
     
     void Start () {
     currentTime = Time.time + timeToIdle;
     }
     
     // Update is called once per frame
     void Update () {
     
         if(!idle) //this can be replaced by something to check if there is no input etc..
         {
             checkIdle();
         }
     }
     
     void checkIdle()
     {
         if(Time.time > currentTime)
         {
             idle = true;
             //run your anim here or a seperate function to translate the boolean value
             currentTime = Time.time + timeToIdle;
         }
     }
 }
 
Your answer
 
 
             Follow this Question
Related Questions
Can StateMachineBehaviour use root transform? 1 Answer
Animation Event and Sate Machine Behaviour 0 Answers
Detecting last frame of animation, without StateMachineBehaviours or Animation Events 0 Answers
Some parts of animation not playing 0 Answers
Can an empty state in Animator cause performance issues? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                