Shoot delay with Time.time-if-function does not work [solved]

Hello there,

I’ve read several threads about delaying a shot in unity. The answer is almost the same.

Here is my Code:

  private void Update()
    {
        if (Input.GetButton("Fire1") && Time.time > FireRateTimer)
        {
            Debug.Log(FireRateTimer);

            GameObject ProjectileVar = (GameObject)Instantiate(Projectile, projectileSpawn.position, Quaternion.identity);

            Vector2 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 myPos = new Vector2(transform.position.x, transform.position.y + 1);
            Vector2 direction = target - myPos;

            direction.Normalize();
            ProjectileVar.GetComponent<Rigidbody2D>().AddForce(direction * 4000);
            
            FireRateTimer = Time.time + FireRate;
            SoundTurret.PlayOneShot(ShootSound);
        }
    }

But no matter what number is given to FireRate it still shoots like every 1 seconds. I’ve checked the values with debug command and it’s mostly a second.

I don’t have any clue why this isn’t working because the Time.time thing should be the solution for that, following the threads with that topic.

Code looks OK.

What’s the actual FireRate set to? If you set it in the inspector then updated the value in code, it’s still using the value set in the inspector.

Try Debug.Log(FireRate) and make sure you have the value you expect.

1 Like

This could be a thing, didn’t mentioned that. I’ll check it when I come home.

Thank you for the tip :slight_smile:

Edit: I checked the inspector and this was of course the problem. Everything works as it should be now. Happens when you try to do something while totally tired :slight_smile:

Thanks !!