I would remove that from the Update() function as it would shoot every frame.
using UnityEngine;
using System.Collections;
public class Enemyfireprojectile : MonoBehaviour {
public Transform target; //this is the rigidbody you want the object to shoot at
bool isShooting = false;
public Rigidbody projectile;
void Update() {
if(!isShooting)
{
StartCoroutine(shoot());
}
}
IEnumerator shoot()
{
Rigidbody clone;
transform.LookAt(target)
isShooting = true;
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.forward); //this might be wrong
yield return new WaitForSeconds(1/15); //rate of fire
isShooting = false;
}
it works because you are not firing every frame. By using an IEnermator (coroutine) you stop the bullet from being fired every frame, and force it to wait for 1/15 seconds to be able to shoot again.
using UnityEngine;
using System.Collections;
public class Enemyfireprojectile : MonoBehaviour {
public Transform target; //this is the rigidbody you want the object to shoot at
bool isShooting = false;
public Rigidbody projectile;
void Update() {
if(!isShooting)
{
StartCoroutine(shoot());
}
}
IEnumerator shoot()
{
Rigidbody clone;
transform.LookAt(target);
isShooting = true;
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.forward); //this might be wrong
yield return new WaitForSeconds(1/15); //rate of fire
isShooting = false;
}
{