Search Unity

Fire Rate Issues

Discussion in 'Scripting' started by SpuddySpuddy, Dec 21, 2020.

  1. SpuddySpuddy

    SpuddySpuddy

    Joined:
    Feb 26, 2020
    Posts:
    3
    So I'm new to Unity and been following a lot of Brackey's tutorials to find my way. I want to make some sort of FPS game so I watched "Shooting with Raycasts - Unity Tutorial"

    It all works until I add the fire rate in and then it just seems to not work. When I use the code below, the gun shoots full auto since it is "GetButton" but the fire rate doesn't seem to come into play at all. Can anyone help?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GunScript : MonoBehaviour
    6. {
    7.     public float damage = 25f;
    8.     public float range = 100f;
    9.     public AudioSource gunShot;
    10.     public float fireRate = 1f;
    11.     public Camera fpsCam;
    12.     public ParticleSystem muzzleFlash;
    13.  
    14.     private float nextTimeToFire = 0f;
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.      
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if(Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
    25.         {
    26.             nextTimeToFire = Time.time + 1f / fireRate;
    27.             Shoot();
    28.         }
    29.     }
    30.  
    31.     void Shoot()
    32.     {
    33.         muzzleFlash.Play();
    34.         gunShot.Play();
    35.         RaycastHit hit;
    36.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    37.         {
    38.             Debug.Log(hit.transform.name);
    39.  
    40.             Enemy target = hit.transform.GetComponent<Enemy>();
    41.             if(target != null)
    42.             {
    43.                 target.TakeDamage(damage);
    44.             }
    45.         }
    46.     }
    47. }
    48.  
     
    Last edited: Dec 21, 2020
    Colmod likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    This is commonly called a "cooldown timer." You can google for examples and tutorials, but the simplest way is this:

    1. have a
    private float gunHeat;
    variable

    2. In Update(), if that variable is greater than zero, subtract Time.deltaTime from it (this cools down the
    gunHeat
    )

    Code (csharp):
    1. if (gunHeat > 0)
    2. {
    3.   gunHeat -= Time.deltaTime;
    4. }
    3. When you go to fire:
    --- A. only fire if that
    gunHeat
    variable is zero or less.
    --- B. set that variable to the time (in seconds) you want to have between shots.

    Code (csharp):
    1. if (UserWantsToFireTheGun)
    2. {
    3.   if (gunHeat <= 0)
    4.   {
    5.     gunHeat = 0.25f;  // this is the interval between firing.
    6.     ActuallyDoTheFiring();
    7.   }
    8. }
     
    Last edited: Dec 21, 2020
  3. SpuddySpuddy

    SpuddySpuddy

    Joined:
    Feb 26, 2020
    Posts:
    3
    Seriously thanks. Been trying this many ways and when looking at this way I feel like an idiot. Thanks :)
     
    Kurt-Dekker likes this.
  4. gustmaes

    gustmaes

    Joined:
    Jan 16, 2021
    Posts:
    1
    thx you really helped me
     
    Kurt-Dekker likes this.
  5. alfons1555

    alfons1555

    Joined:
    Sep 23, 2022
    Posts:
    1