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 /
avatar image
0
Question by Aspect13 · Dec 11, 2018 at 07:32 PM · shootingshootshooterreloadreloading

Shooting and reloading mechanism problem.

Maybe at first I should show my script and explain the problem. Okay, so the problem is shooting like 100 times per second. Not literally but It's like super rapid fire gun. When I shoot, the current ammo (30/120, 29/120, 28/120 and so on) goes down till It's -99 or so and then immediately goes back to about -10, slows down and starts to reduce again to -99 and it goes like that forever. The total ammo which is in this case 120 is always the same. The reload coroutine works but I can shoot while reloading and then the ammo resets to 30 here but it does not stop going down and down.

The script looks like this:

 private void Update()
     {
         if (Input.GetKeyDown(KeyCode.R))
         {
             StartCoroutine(Reload());
         }
     }
 
     IEnumerator Reload()
     {
             canShoot = false;
             yield return new WaitForSeconds(2);
             gameManager.GetComponent<GameManager>().AmmoCurrent.text = ammoReload.ToString();
             ammoCurrentInt = ammoReload;
             canShoot = true;
     }
 
 public void Shoot()
     {
         if (isShooting && ammoCurrentInt == 0 && canShoot == false)
             return;
 
         isShooting = true;
         GameObject flash = Instantiate(FlashEffect, Vector3.zero, Quaternion.identity);       
         flash.transform.SetParent(BulletEmiter);
         flash.transform.localPosition = Vector3.zero;
         flash.transform.localRotation = BulletEmiter.localRotation;
 
 
         ammoCurrentInt--;
         gameManager.GetComponent<GameManager>().AmmoCurrent.text = ammoCurrentInt.ToString();
 
 
         SoundSource.PlayOneShot(ShootSound);
 
         Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * 200f, Color.red, 2f);
 
         RaycastHit hit;
         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 200f))
         {
             HitDetected(hit);
         }
         StartCoroutine(AutoFire());
     }
     IEnumerator AutoFire()
     {
         yield return new WaitForSeconds(shootDelay);
         isShooting = false;
     }

Okay, maybe now I should describe some of my variables as they can be confusing.


ammoReload is int, It's used to reload the gun and It's never changed. It's set to ammoCurrentInt which is the variable to display ammo which is left on the screen.


When you reload you can't shoot that is why I made canShoot variable. The coroutine AutoFire() is to make delays between shots.


If It's understable then sorry for this quite long explanation. I just made it because I wouldn't understand it if someone showed me this lol.

Comment
Add comment · Show 3
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 Vega4Life · Dec 11, 2018 at 07:34 PM 0
Share

Where is shoot being called?

avatar image Aspect13 Vega4Life · Dec 11, 2018 at 07:37 PM 0
Share

In the PlayerController script, attached to player:

 if (Input.Get$$anonymous$$ouseButton(0))
         {
             playerShooter.Shoot();
         }
avatar image Vega4Life Aspect13 · Dec 11, 2018 at 08:02 PM 0
Share

I feel like this


 if (isShooting && ammoCurrentInt == 0 && canShoot == false)


Should be this


 if (ammoCurrentInt == 0 || canShoot == false)



If it isn't... then as long as you have ammo you can ALWAYS shoot. Even while reloading, even if you don't have ammo or can shoot. This is a bad line here. I would try what I wrote and see what kind of changes you get.

1 Reply

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

Answer by berndunity · Dec 11, 2018 at 07:43 PM

          isShooting = true;
          GameObject flash = Instantiate(FlashEffect, Vector3.zero, Quaternion.identity);       
          flash.transform.SetParent(BulletEmiter);
          flash.transform.localPosition = Vector3.zero;
          flash.transform.localRotation = BulletEmiter.localRotation;

You're never asking if canShoot is true, so this part is always called when the Shoot() method is called. I would change it to:

 if (isShooting == false && ammoCurrentInt >= 1 && canShoot){
    //Now you can shoot.
 }


Comment
Add comment · Show 5 · 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 Aspect13 · Dec 11, 2018 at 07:52 PM 0
Share

I'm asking for it here, at the beginning of the Shoot() function, if It's false, you can't shoot and if It's true, you can:

 public void Shoot()
      {
          if (isShooting && ammoCurrentInt == 0 && canShoot == false)
              return;
avatar image berndunity · Dec 11, 2018 at 07:52 PM 0
Share
 if (isShooting && ammoCurrentInt == 0 && canShoot == false)
              return;

This part should be changed to

 if(isShooting || ammoCurrentInt <= 0 || canShoot == false)
 return;

Because you don't want to shoot if isShooting is true or if the ammo is = or lower then 0 or if canShoot is false, because you're reloading.

avatar image Aspect13 berndunity · Dec 11, 2018 at 07:58 PM 0
Share

Unfortunately, still the same. It didn't fix the problem.

EDIT: wait I forgot to change the "&&" symbols. I will try now. EDIT 2: It worked! Thanks, simple mistake but couldn't figure it out.

avatar image Vega4Life berndunity · Dec 11, 2018 at 08:05 PM 0
Share

It can't be that because, yes you could have no ammo, but canshoot could still be true, thus it still tries to shoot without ammo.


  if (ammoCurrentInt == 0 || canShoot == false)



Should be this I think. I don't like less than here for ammo count because if it does get to less than zero, I want to know about it. That means a bug.

avatar image berndunity · Dec 11, 2018 at 08:02 PM 0
Share

Nice well done! Can you please vote up the answer I need points ;).

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

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to shoot a bullet to the mouse cursor position? 1 Answer

shooting problem with raycasting 1 Answer

Shoot Script Help 1 Answer

Problem with firing laser in direction of camera 1 Answer

FPS rigidbody bullets not moving to center of screen 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