- Home /
How do I stop my enemies from rapidfiring?
I'm attempting to create an fps enemy, but I am having an irritating bug. The enemies are shooting without delay, causing close combat to be impossible. If you can be help: Here is the enemy shooting script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Sight : MonoBehaviour { TargetPlayer TP;
GameObject Player;
public GameObject SightOwner;
public float Damage = 50f;
public ParticleSystem MuzzleFlash;
public GameObject Emitter;
public Rigidbody Bullet;
public Transform Spawn;
public int Stranth = 5000;
bool Shot = false;
public float fireRate = 0.5f;
GameObject AudioManage;
AudioManager AudioSounder;
// Use this for initialization
void Start ()
{
Player = GameObject.FindGameObjectWithTag ("Player");
TP = SightOwner.GetComponent<TargetPlayer> ();
AudioManage = GameObject.Find ("AudioManager");
AudioSounder = AudioManage.GetComponent<AudioManager> ();
}
void Update()
{
}
IEnumerator ShootDelay()
{
yield return new WaitForSeconds (fireRate);
}
// Update is called once per frame
void OnTriggerEnter ()
{
TP.enabled = true;
}
void OnTriggerStay (Collider Col)
{
if (Col.tag == "Player")
{
SightOwner.transform.LookAt (Player.transform.position);
if (Shot == false)
{
ShootAtPlayer ();
}
else if (Shot == true)
{
StartCoroutine (ShootDelay());
}
}
}
void OnTriggerExit ()
{
}
void ShootAtPlayer()
{
MuzzleFlash.Play ();
Rigidbody DaBullet;
DaBullet = Instantiate (Bullet, Spawn.position, Spawn.rotation) as Rigidbody;
Shot = true;
DaBullet.AddForce (Spawn.forward * Stranth);
AudioSounder.Play("MusketSound");
Destroy (DaBullet, 2);
Debug.Log ("Pew");
}
}
Here's a video: https://youtu.be/BKv-I4vC6Ow Any help is appreciated :)
Answer by EDevJogos · Nov 21, 2017 at 10:30 PM
I would recommend for you to take a look again on how coroutines work, but for the time being this should fix it.
IEnumerator ShootDelay()
{
yield return new WaitForSeconds (fireRate);
Shot = false; //Add this.
}
void OnTriggerStay (Collider Col)
{
if (Col.tag == "Player")
{
SightOwner.transform.LookAt (Player.transform.position);
if (Shot == false)
{
ShootAtPlayer ();
}
/*else if (Shot == true) Remove this.
{
StartCoroutine (ShootDelay());
}*/
}
}
void ShootAtPlayer()
{
MuzzleFlash.Play ();
Rigidbody DaBullet;
DaBullet = Instantiate (Bullet, Spawn.position, Spawn.rotation) as Rigidbody;
DaBullet.AddForce (Spawn.forward * Stranth);
AudioSounder.Play("MusketSound");
Destroy (DaBullet, 2);
Debug.Log ("Pew");
Shot = true;
StartCoroutine (ShootDelay()); // Add this.
}
Thank you for replying. I agree that I need to do more research, but I am still confused on what is causing this problem. I tried what you sent me(thank you again for it), but the problem is still there. Do you know why it is happening, should I rebuild this part differently?
Did you copy and paste @Search's code or did you try to make the changes by hand?
If you made them by hand, make sure you put in the StartCoroutine(ShootDelay()); line into ShootAtPlayer.
Oh, wait... never $$anonymous$$d. I just had to adjust where the coroutine started and where shot equaled true. Thx.
Your answer
Follow this Question
Related Questions
Enemy doesn't shoot 1 Answer
OnTriggerEnter2D with multiple objects 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers