Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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): alt text

snip-1-1-1.png (15.3 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Nov 09, 2015 at 07:05 AM 0
Share

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.

avatar image General-Troll rutter · Nov 09, 2015 at 10:30 PM 0
Share

@rutter that is the point of the antiSpam variable, yet it still repeats 5 times.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

How do I change the size in Input Manager? 1 Answer

Efficient way to use UGui.Overlap for a GUI Mouse 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges