- Home /
Firerate problem
The var "sta_sparando" remains on after i release left mouse button
#pragma strict
var suono_sparo : AudioClip;
var munizioni = 100;
var firerate = 0.1;
var sta_sparando=false;
var muzzleflash : ParticleSystem;
function Start () {
muzzleflash = muzzleflash.GetComponent.<ParticleSystem>();
muzzleflash.Stop(true);
}
function Update() {
if(Input.GetMouseButton(0)&&sta_sparando==false){
fire();
}
}
function fire(){
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
muzzleflash.Play(true);
sta_sparando=true;
print("boom");
AudioSource.PlayClipAtPoint(suono_sparo, transform.position);
if (Physics.Raycast(ray, hit)){
Debug.DrawLine(ray.origin, hit.point);
if (hit.transform.tag == "Zombie"){
var zombie_colpito = hit.collider.gameObject;
var zombie_life=zombie_colpito.GetComponent(AiZombie);
zombie_life.salute-=15;
print("Zombie colpito");
}
if (hit.transform.tag == "Cadavere_zombie"){
var cadavere_colpito = hit.collider.gameObject;
var rb = cadavere_colpito.GetComponent(Rigidbody);
rb.AddForce(transform.forward * 10,ForceMode.Impulse);
print("Cadavere colpito");
}
}
yield WaitForSeconds(firerate);
muzzleflash.Stop(true);
sta_sparando=false;
}
Answer by Ali-hatem · Apr 17, 2018 at 10:20 AM
use Input.GetMouseButtonUp to set sta_sparando to false if you are trying to fire only once per click :
if (Input.GetMouseButtonUp(0))
sta_sparando = false;
if so you don't need a (bool & Input.GetMouseButtonUp) just use Input.GetMouseButtonDown it will call only once even if you hold mouse button.but if you want to keep holding click & wait between fire again add debug log after the yield code i don't use js in c# wait should bee in a coroutine.
Before the last unity update, the script worked correctly...... why? however, I did not solve the problem.
i have tested your code with version (unity 2017.3) and it work fine just simplify your script to know where is the problem :
#pragma strict
var sta_sparando = false;
function Update(){
if(Input.Get$$anonymous$$ouseButton(0)&&sta_sparando==false){
fire();
}
}
function fire(){
print("true");
sta_sparando = true;
yield WaitForSeconds(0.1);
sta_sparando = false;
print("false ");
}
Answer by Franklin96 · Apr 19, 2018 at 02:37 PM
Thanks, now it work :D #pragma strict var suono_sparo : AudioClip; var munizioni = 100; var firerate = 0.1; var sta_sparando=false; var muzzleflash : ParticleSystem; function Start () { muzzleflash = muzzleflash.GetComponent.(); muzzleflash.Stop(true); } function Update(){ if(Input.GetMouseButton(0)&&sta_sparando==false){ fire(); } } function fire(){ var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit; muzzleflash.Play(true); if (Physics.Raycast(ray, hit)){ Debug.DrawLine(ray.origin, hit.point); if (hit.transform.tag == "Zombie"){ var zombie_colpito = hit.collider.gameObject; var zombie_life=zombie_colpito.GetComponent(AiZombie); zombie_life.salute-=15; print("Zombie colpito"); } } print("true"); sta_sparando = true; yield WaitForSeconds(0.1); muzzleflash.Stop(true); sta_sparando = false; print("false "); }
Your answer
