Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by RedTwentyFour · Jul 27, 2014 at 10:36 AM · weaponienumeratorfirereloadinvokerepeating

Weapon reload timer problem.

i tried to write a weapon fire script, that also has an automatic reload timer, it works well, shoots the bullets, substracts 1 ammunition, and then waits X seconds until it can fire again with the ammo reloaded. The problem is that when it reloaded and has X amount of bullets again, it wont substract anymore, so the second round is infinite. any ideas what i am doing wrong?

 using UnityEngine;
 using System.Collections;
 
 public class NewWeaponManager : MonoBehaviour {
 
     public GameObject parent;
     public GameObject muzzle;
     public Texture[] muzzleTexture;
     public Rigidbody bulletPrefab;
     public bool fireWeapon;
     public float velocity = 10.0f;
     public float weaponSpeed = 0.1f;            // Speed of weapon.
     public float weaponScattering = 0.005f;
     public bool isSpawning = false;
 
     public int currentAmmunition;
     public int maxAmmunition = 25;
 
     public bool muzzleFlashEnabled = false;
 
     public bool reloadWeapon = false;
 
     // Use this for initialization
     void Start () {
     
         currentAmmunition = maxAmmunition;
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (muzzleFlashEnabled == true) {
             muzzle.gameObject.SetActive(true);
         } else {
             muzzle.gameObject.SetActive(false);
         }
 
         if (currentAmmunition <= 0) {
             reloadWeapon = true;
             StartCoroutine (ReloadWeapon (5));
             CancelInvoke("SpawnBullet");
         }
 
         if (fireWeapon == true && isSpawning == false) {
             if (reloadWeapon == false) {
                 muzzleFlashEnabled = false;
                 isSpawning = true;
                 InvokeRepeating ("SpawnBullet", 0.001f, weaponSpeed);
             }
         } else if (fireWeapon == false) {
             muzzleFlashEnabled = false;
             CancelInvoke("SpawnBullet");
             isSpawning = false;
         }
     
     }
 
     void SpawnBullet() {
 
         muzzleFlashEnabled = true;
         float randomDirX = Random.Range (-weaponScattering, weaponScattering);
         float randomDirY = Random.Range (-weaponScattering, weaponScattering);
         Quaternion ScatterDirection = new Quaternion (transform.rotation.x + randomDirX, transform.rotation.y + randomDirY, transform.rotation.z, transform.rotation.w);
 
         currentAmmunition -= 1;
 
         Rigidbody newBullet = Instantiate(bulletPrefab, transform.position, ScatterDirection) as Rigidbody;
         newBullet.AddForce(transform.forward*velocity, ForceMode.VelocityChange);
         newBullet.GetComponent<Ammunition>().parentWeapon = gameObject;
 
     }
 
     public IEnumerator ReloadWeapon(float timer) {
         yield return new WaitForSeconds (timer);
         reloadWeapon = false;
         currentAmmunition = maxAmmunition;
     }
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by smallbit · Jul 27, 2014 at 11:03 AM

I am not sure but I think the problem might be in this part

 if (currentAmmunition <= 0) {
             reloadWeapon = true;
             StartCoroutine (ReloadWeapon (5));
             CancelInvoke("SpawnBullet");
         }

I don't have access to unity to test, but I think you are launching the reload coroutine every frame until the first one actually get to the line of code (it happens after 5 seconds)

 currentAmmunition = maxAmmunition;

because till than it will be still zero. Quick fix might be to add quick check like this

  if (currentAmmunition <= 0 && !reloadWeapon) {
             reloadWeapon = true;
             StartCoroutine (ReloadWeapon (5));
             CancelInvoke("SpawnBullet");
         }


Again have no way to check the code now, put debug.log in your reload coroutine to see how many times it is called.

Comment
Add comment · Share
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
0

Answer by RedTwentyFour · Jul 28, 2014 at 08:28 AM

This works perfectly, thank you!

Comment
Add comment · Share
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

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

press a key, timing sequence? 0 Answers

Fire only if you don't reload 1 Answer

c# weapon shooting script using raycasting help 1 Answer

Failed to inplement smooth increment of weapon trail between frames in Mecanim animation 0 Answers

Reload manually in raycast? 1 Answer


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