I have a trouble, that sometimes my “gun” object fires twice, with about 0.1 second cooldown.
I dont know how to fix that problem, I implemented 2 types of bullet cooldown using Time.time and making variable that lose 1/50f every FixedUpdate frame, but no one of this systems doesn’t work properly. Help me please.
“Player_shoot code”:
public GameObject bullet;
public GameObject enemy;
public float bulletCd = 1f;
public int damage = 1;
private float shootTimer = 0;
void Update()
{
if (Input.GetMouseButton(0) && shootTimer <= Time.time) {
shootTimer = Time.time + bulletCd;
Fire();
}
if (Input.GetMouseButtonDown(1)) {
SpawnEnemy();
}
}
void Fire() {
bullet.transform.position = transform.position;
bullet.transform.rotation = transform.rotation;
bullet.gameObject.SendMessage("SetDamage", damage);
Instantiate(bullet);
}