public float timeBetweenShots = 5.0f;
public float bulletLifeTime = 10.0f;
public bool shoot = true;
public float lifeTime = 5.0f;
public Rigidbody Enemybullet;
public Transform muzzlepoint;
public float minDist = 10;
public float maxDist = 5;
public float speed;
private bool follow; // Follow Enemy Switch
void Update()
{
Attack(); }
// Enemy Attack Function
private void Attack()
{
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();// Shooting Function is called
if(Vector3.Distance(transform.position,target.position)<= minDist)
{
Shooting();
}
}
}
// Start shooting Function
void Shooting()
{
shoot = true;
StartCoroutine("TimerShoot");
}
// Timed Shooting
IEnumerator TimerShoot()
{
while(shoot)
{
if(Enemybullet)
{
Rigidbody newEnemyBullet;
newEnemyBullet = Instantiate(Enemybullet,muzzlepoint.position,muzzlepoint.rotation) as Rigidbody; // casting and Instantiating bullet.
Physics.IgnoreCollision(collider, newEnemyBullet.collider);
Destroy(newEnemyBullet.gameObject, bulletLifeTime);
yield return new WaitForSeconds(timeBetweenShots);
}
}
}
I have many ways but still the bullets are emitted continuously. Is there any way to fix it?,I want the bullet to shoot few times not continuously. Please help. Is there any way that I can stop the enemy to stop following the player once it reached the min distance? AM a newbie how to fix this. Please help.Thanks in advance.