Firing Improvements

my coding is the same as on course however, my projectiles fire rate is more like 20 seconds, is this the intended delay, in the video it seems to be a much shorter delay.

Hi there,
Can you share your firing code?

‘’’
{

[Header("References")]

[SerializeField] private InputReader inputReader;

[SerializeField] private GameObject clientProjectile;

[SerializeField] private GameObject serverProjectile;

[SerializeField] private Transform projectileSpawnPoint;

[SerializeField] private GameObject muzzleFlash;

[SerializeField] private Collider2D playerCollider;

[Header("Settings")]

[SerializeField] private float projectileSpeed;

[SerializeField] private float muzzleFlashDuration;

[SerializeField] private float fireRate;

private bool shouldFire;

private float previousFireTime;

private float muzzleFlashTimer;

//Subscribe to the fire event when key is pressed.

public override void OnNetworkSpawn()

{

    if(!IsOwner){return;}

    inputReader.PrimaryFireEvent += HandlePrimaryFire;

}

//Unsubscribe to the event when the key is unpressed.

public override void OnNetworkDespawn()

{

    if (!IsOwner) { return; }

    inputReader.PrimaryFireEvent -= HandlePrimaryFire;

}

private void Update()

{

    //setting muzzleFlashTimer

    if(muzzleFlashTimer > 0f)

    {

        muzzleFlashTimer -= Time.deltaTime;

        if(muzzleFlashTimer <= 0f)

        {

            muzzleFlash.SetActive(false);

        }

    }

    //logic involving spawning and fireRate.

    if (!IsOwner) { return; }

   

    if(!shouldFire){return;}

    if(Time.time < (1 / fireRate) + previousFireTime){return;}

    PrimaryFireServerRpc(projectileSpawnPoint.position, projectileSpawnPoint.up);

    SpawnDummyProjectile(projectileSpawnPoint.position, projectileSpawnPoint.up);

    previousFireTime = Time.time;

}

//Because the controls for the input are set as a bool, shouldFire. This method is called by the event and sets the variable.

private void HandlePrimaryFire(bool shouldFire)

{

    this.shouldFire = shouldFire;

}

//this is the server RPC, this spawns the projectile for the server to handle logic on the server side.

[ServerRpc]

private void PrimaryFireServerRpc(Vector3 spawnPos, Vector3 direction)

{

    GameObject projectileInstance = Instantiate(

        serverProjectile,

        spawnPos,

        Quaternion.identity);

   

    projectileInstance.transform.up = direction;

    Physics2D.IgnoreCollision(playerCollider, projectileInstance.GetComponent<Collider2D>());

    if(projectileInstance.TryGetComponent<DealDamageOnContact>(out DealDamageOnContact dealDamage))

    {

        dealDamage.SetOwner(OwnerClientId);

    }

    if (projectileInstance.TryGetComponent<Rigidbody2D>(out Rigidbody2D rb))

    {

        rb.velocity = rb.transform.up * projectileSpeed;

    }

    SpawnDummyProjectileClientRpc(spawnPos, direction);

}

//This is the ClientRPC, this allows the other players (Not the owner) see the projectile on their side.

[ClientRpc]

private void SpawnDummyProjectileClientRpc(Vector3 spawnPos, Vector3 direction)

{

    if(IsOwner){return;}

    SpawnDummyProjectile(spawnPos,direction);

}

//this controls the visuals for the projectile and also controls the movement of the projectile.

private void SpawnDummyProjectile(Vector3 spawnPos, Vector3 direction)

{

    muzzleFlash.SetActive(true);

    muzzleFlashTimer = muzzleFlashDuration;

    GameObject projectileInstance = Instantiate(

        clientProjectile,

        spawnPos,

        Quaternion.identity);

    projectileInstance.transform.up = direction;

    Physics2D.IgnoreCollision(playerCollider, projectileInstance.GetComponent<Collider2D>());

    if(projectileInstance.TryGetComponent<Rigidbody2D>(out Rigidbody2D rb))

    {

        rb.velocity = rb.transform.up * projectileSpeed;

    }

}

}
‘’’

Okay looks good, and what do you have the fire rate set to in the editor? It’s Serialized, so the inspector value will override defaults in the script.

Remember, fireRate is projectiles per second. It’s the inverse of the time between firing which would be seconds per projectile.
fireRate [1/sec]
fireTime [sec]

0.075

So 1 / 0.075 = 13.33 seconds. So you will get 13 seconds between projectiles. If you want to lower that amount of time, increase the firing rate.

Thank you I was slightly confused because the fire rate in the videos seemed lower but he had the same configurations as myself… thank you though I will likely just increase the Rate.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms