I have a problem in instantiating the bullet for every few seconds. I have achieved this effect with player control. When I press key down each time the bullet is been called. But how to automate this function in enemy?
I have introduce yield return, the bullets(particles) are been emitted continuously. But I want it for each seconds (single instantiation) or somewhat like that.
{
public Transform target;
public float maxRange;
public float minRange;
public float rotationSpeed;
public float timeBetweenShots = 5.0f;
public float bulletLifeTime = 10.0f;
public bool shoot = true;
public Rigidbody Enemybullet;
public Transform muzzlepoint;
public float speed;
private bool follow; // Follow Enemy Switch
void Update()
{
Attack();
}
private void Attack() // Enemy Attack Function
{
if((Vector3.Distance(transform.position,target.position)<maxRange)
&& (Vector3.Distance(transform.position,target.position)>minRange))
{
follow = true; // Enemy Switch Enabled
}
if(follow) // On enemy Switch Enabled
{
transform.LookAt(target);// Locking the target
transform.Translate(Vector3.forward *speed* Time.deltaTime);// Forward Motion
transform.Rotate(0,90,0); // Gimber Lock
Shooting();// Shotting Function is called
}
}
void Shooting()
{
shoot = true;
StartCoroutine("TimerShoot");
}
IEnumerator TimerShoot()
{
while(shoot)
{
if(Enemybullet)
{
Rigidbody newEnemyBullet;
newEnemyBullet = Instantiate(Enemybullet,muzzlepoint.position,muzzlepoint.rotation) as Rigidbody;
Physics.IgnoreCollision(collider, newEnemyBullet.collider);
Destroy(newEnemyBullet.gameObject, bulletLifeTime);
yield return new WaitForSeconds(timeBetweenShots);
}
}
}
}