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();

I finally was able to figure it out by looking deeper into the LookAt function. Thanks so much for pointing me in the right direction! I would give you points but I have none. lol Here is how I was able to get it to work. I added this code after I set a new waypoint: Quaternion rotation = Quaternion.LookRotation(waypoints[cur].transform.position - transform.position, transform.TransformDirection(Vector3.up)); transform.rotation = new Quaternion(0, 0, rotation.x, rotation.w);

1 Answer

1

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.