- Home /
Question by
Jigglebones · Jan 05, 2014 at 06:53 AM ·
gunwait
Need help with gun script to reduce the gun's rate of fire
I am making a revolver. Everything is working as it should, except the rate of fire is way too high. If I click repeatedly the gun fires fully automatically. How do I make the script wait for, lets say two seconds, before it can fire again? Ammo and reloading have not been implemented yet. Here is my script:
#pragma strict
var impactEffect : Transform;
var fireSmoke : ParticleSystem;
var fireFlash : GameObject;
var Damage = 100;
var Range = 100;
function Update ()
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.51, Screen.height*0.51, 0));
if (Input.GetMouseButtonDown(0))
{
animation.Play("RevAttack");
audio.Play();
Flash();
if (Physics.Raycast (ray, hit, Range))
{
var particleClone = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
function Flash ()
{
if (Input.GetMouseButtonDown(0))
{
fireFlash.SetActiveRecursively(true);
fireSmoke.Play();
yield WaitForSeconds (0.1);
fireFlash.SetActiveRecursively(false);
yield WaitForSeconds (0.5);
fireSmoke.Stop();
}
}
Comment
Best Answer
Answer by Seth-Bergman · Jan 05, 2014 at 07:06 AM
first off, you can get rid of the if statement in the Flash function:
function Flash ()
{
fireFlash.SetActiveRecursively(true);
...etc since it is only called if mouse(0) is pressed anyway, and besides you can only really use GetButtonDown inside Update() (for all practical purposes)
as for a pause between shots.. one easy way is to create a boolean for that, for example:
var impactEffect : Transform;
var fireSmoke : ParticleSystem;
var fireFlash : GameObject;
var Damage = 100;
var Range = 100;
var canShoot = true;
function Update ()
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.51, Screen.height*0.51, 0));
if (Input.GetMouseButtonDown(0) && canShoot)
{
canShoot = false;
GunReset(2);
animation.Play("RevAttack");
audio.Play();
Flash();
if (Physics.Raycast (ray, hit, Range))
{
var particleClone = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
function GunReset(pauseTime : float){
yield WaitForSeconds(pauseTime);
canShoot = true;
}