- Home /
Raycast Gun Fire Rate
Making an aim trainer app in unity 2017.4.17f1 personal for mobile devices. made a script that shoots every time you click:
void Update ()
{
if (Input.GetButtonDown ("Fire1"))
{
Shoot ();
}
}
It worked but you had to click each time you wanted to shoot, which is annoying. so, I modified it to the following:
void Update ()
{
if (Input.GetButton ("Fire1"))
{
Shoot ();
}
}
It worked as in it would constantly shoot when you hold the button, but it didn't have a rate at which it fired, so i modified it again to the following:
public float fireRate = 0.5f;
public float damage = 10f;
public float range = 100f; //not being used yet
public Camera fpsCam;
public ParticleSystem muzzleFlash;
void Update ()
{
if (Input.GetButton ("Fire1") && Time.time > fireRate)
{
Shoot ();
}
}
Now it doesn't work at all, and i am a bit lost as to what i should do. Here is the Shoot script if it helps:
void Shoot()
{
muzzleFlash.Play (); //Just a particle system
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target> ();
if (target != null)
{
target.TakeDamage (damage);
}
}
}
(p.s. I am pretty new to all this stuff so I don't yet know what a lot of the terms and words mean that I've seen in other places)
,Trying to make an app for a school assignment, decided to make a mobile aim trainer for things like COD:M etc. I had made a script that worked to shoot every time you clicked using:
if (input.GetButtonDown ("Fire1")) { Shoot(); }
but to shoot you had to click for every shot. so i changed input.GetButtonDown to input.GetButton. while this did make it so you could just hold the button down to shoot multiple times, but it was firing way too fast, so i modified the script to this, and now it doesn't work at all.
public float fireRate = 0.5f;
public float damage = 10f;
public float range = 100f; //not being used yet
public Camera fpsCam;
public ParticleSystem muzzleFlash;
void Update ()
{
if (Input.GetButton ("Fire1") && Time.time > fireRate)
{
Shoot ();
}
}
Any idea on what i should do? (P.S. im pretty new to C# so please be a bit mindful of the terms you use)
Your answer
![](https://koobas.hobune.stream/wayback/20220612225611im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Reload clips on Gun-script not applied properly 1 Answer
Gun locator script isnt working 1 Answer
How can i make a ray cast take health from enemies 2 Answers
Raycast Projectile w/ Physics 2 Answers
Help getting Raycast last hit data 2 Answers