- Home /
 
 
               Question by 
               maroonswordsman · Jul 12, 2016 at 08:03 AM · 
                inputcoroutinewaitforsecondsienumerator  
              
 
              Delay after input?
Hello am trying to make a shooting game in which when a key, for example space, is pressed the game waits for 0.3 seconds before the gun shoots because there is an animation that needs to played before that the bullet leaves the gun. Here is a code that I have been trying to use:
 void Start()
     {
         StartCoroutine ("Shoot");
     }
 
     IEnumerator Shoot()
     {
         yield return new WaitForSeconds (0.3f);
         Bam ();
     }
 
     void Bam()
     {
         transform.Translate (0, 0, 12);
     }
 
     void Update () {
 
         if (Input.GetKey (KeyCode.Space))
              Shoot();
             
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by MUGIK · Jul 12, 2016 at 11:48 AM
If u use animation, u can use Animation Events: https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html . About ur code:
  void Start()
      {
         // NOTHING
      }
  
      IEnumerator Shoot()
      {
          _isShooting = true;
          yield return new WaitForSeconds (0.3f);
          Bam ();
          _isShooting = false;
      }
  
      void Bam()
      {
          transform.Translate (0, 0, 12);
      }
  [SerializeField] bool _isShooting; // while u wait 0.3 sec for shoot u cant shoot again
      void Update () {
          if (Input.GetKey (KeyCode.Space) && !_isShooting)
               StartCoroutine( Shoot());
 }
 
               But I advise u use Animation Events.
Your answer
 
             Follow this Question
Related Questions
WaitForSeconds Not Working 4 Answers
Ienumerator wait for event 0 Answers
IEnumerator manipulates variable in android build 1 Answer
wait a second before say path is valid? 2 Answers
yield return waitforseconds not waiting? 3 Answers