her in this code I. have a line code which is responsible for the shooting time but it seems to be not working . the problem I have here is as the time goes on the firing rate get higher and higher .
let’s have a look on. my code
here is the code responsible for the timings
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Rendering;
public class gun : MonoBehaviour
{
public float damage = 5f;
public float range = 120f;
public ParticleSystem particleSystem;
public GameObject impactEffect;
[SerializeField]public float firingRate = 30f;
private float nextTimeTofire = 0f;
public int CurrentAmmo = 30;
public int magAmmo = 45;
public bool isReloding;
public Camera fpsCamera;
public AudioSource audioSource;
private void Start()
{
CurrentAmmo = magAmmo;
}
// Update is called once per frame
void Update()
{
if (CurrentAmmo <= 0)
{
isReloding = true;
StartCoroutine(Reload());
}
if (Input.GetButton("Fire1") && Time.time >= nextTimeTofire && !isReloding)
{
audioSource.Play();
for (int i = 0; i < 2; i++)
{
nextTimeTofire = Time.time+(1f / nextTimeTofire);
Debug.Log(nextTimeTofire);
Debug.Log(Time.time);
shoot();
}
}
}
void shoot()
{
CurrentAmmo--;
particleSystem.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCamera.transform.position,fpsCamera.transform.forward,out hit,range))
{
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
target.GetComponent<Rigidbody>().AddForce(hit.transform.position.z*10f,hit.transform.position.y*10f,hit.transform.position.z*10f);
}
var impact = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impact,0.03f);
}
}
IEnumerator Reload()
{
yield return new WaitForSeconds(1f);
isReloding = false;
CurrentAmmo = magAmmo;
}
}
the code responsible for timings
for (int i = 0; i < 2; i++)
{
// this line nextTimeTofire = Time.time+(1f / nextTimeTofire);
Debug.Log(nextTimeTofire);
Debug.Log(Time.time);
shoot();
}