Question by
General-Troll · Nov 09, 2015 at 07:04 AM ·
inputreloadgun scriptrepeatingspam
Reload Function Being Called Multiple Times
So I've been trying to implement an Assault Rifle into the game I am making. Everything works fine, except for the reload function. For some odd reason, the reload function gets called multiple times (around 5 times per button press I think). Please Help!
var soundDraw : AudioClip;
var soundFire : AudioClip;
var soundClipIn : AudioClip;
var soundClipOut : AudioClip;
var mainCamera : Camera;
var crosshairTexture : Texture2D;
var ammo = 20;
var maxAmmo = 20;
var reloadAmmo = 20;
var clip = 100;
var Effect : Transform;
var TheDammage = 100;
var ammotext : GUIText;
var noAmmo : GUIText;
var antiSpam = true;
var bulletTex : GameObject;
var fireRate : float = 0.2;
private var nextFire : float = 0.0;
function Start() {
Draw();
}
function Update () {
if(ammo == 0 && antiSpam == true && clip > 0) {
Reload();
antiSpam = false;
}
Screen.showCursor = false;
reloadAmmo = maxAmmo - ammo;
if(clip < maxAmmo && clip <= reloadAmmo)
reloadAmmo = clip;
audio.pitch = Time.timeScale;
if(Input.GetMouseButton(0) && ammo > 0 && Time.time > nextFire) {
nextFire = Time.time + fireRate;
audio.Stop();
audio.PlayOneShot(soundFire);
animation.Stop();
animation.Play("Fire");
Fire();
ammo -= 1;
}
if(Input.GetMouseButton(1)){
mainCamera.fieldOfView -= 40 * Time.deltaTime/0.3;
if(mainCamera.fieldOfView < 40){
mainCamera.fieldOfView = 40;
}
} else {
mainCamera.fieldOfView += 60 * Time.deltaTime/0.5;
if(mainCamera.fieldOfView > 60){
mainCamera.fieldOfView = 60;
}
}
if(Input.GetKeyDown("r") && ammo < 20 && clip >= 1 && antiSpam == true) {
antiSpam = false;
Reload();
}
}
function Reload () {
Debug.Log("Called");
animation.CrossFade("Reload");
yield WaitForSeconds (1.1);
audio.PlayOneShot(soundClipOut);
yield WaitForSeconds(.217);
ammo = ammo + reloadAmmo;
clip = clip - reloadAmmo;
antiSpam = true;
audio.Stop();
audio.PlayOneShot(soundClipIn);
}
function Draw () {
animation.CrossFade("TakeOut");
audio.PlayOneShot(soundDraw);
}
function OnGUI () {
if(mainCamera.fieldOfView <= 45){
GUI.DrawTexture (Rect(Screen.width/2 - 32/2, Screen.height/2 - 32/2, 32, 32), crosshairTexture);
} else {
GUI.DrawTexture (Rect(Screen.width/2 - 24/2, Screen.height/2 - 24/2, 24, 24), crosshairTexture);
}
ammotext.text = ammo+"|"+clip;
if(clip == 0 && ammo == 0) {
noAmmo.text = "No Ammo";
}
else {
noAmmo.text = " ";
}
}
function Fire() {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Physics.Raycast (ray, hit, 100) && hit.transform.tag == "Enemy")
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
if(Physics.Raycast (ray, hit, 100) && hit.transform.tag == "Land"){
var bulletHole = Instantiate(bulletTex, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
}
}
Picture of Console Log (one reload):
snip-1-1-1.png
(15.3 kB)
Comment
I only skimmed that wall of code, but: you may want to set a flag while you're reloading, so that you don't call Reload
again until it has finished.
@rutter that is the point of the antiSpam variable, yet it still repeats 5 times.
Your answer
Follow this Question
Related Questions
Problem with the gun reloading 0 Answers
What is wrong with my code? (Weapon Reload) 0 Answers
Problems with touch input. 0 Answers