I am currently working on a script for the enemy to shoot instantiated bullet prefabs at me, I have everything working well except being able to set a rate of fire on my script, i am trying to use a waitforseconds function but i dont think i am doing it properly and need pointing in the right direction.
The code is supposed to wait 5 seconds between each shot.
thanks for any help.
using UnityEngine;
using System.Collections;
public class EnemyShootingScript : MonoBehaviour {
public bool ShootAtPlayer = false;
public Transform barrelEnd;
public Rigidbody Bullet;
public float bulletSpeed;
public Animation Recoil;
public bool allowFire;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (ShootAtPlayer&&allowFire)
{
allowFire = false;
Instantiate(Bullet, barrelEnd.position, barrelEnd.rotation);
GetComponent<Animation>().Play("Recoil");
GetComponent<AudioSource>().Play();
new WaitForSeconds(5);
allowFire = true;
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
ShootAtPlayer = true;
}
}[code]