Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 DayyanSisson · Sep 24, 2011 at 12:21 AM · shootingshootlimitbulletslimitations

Shooting with Limited Ammo

I have my shooting script, and to limit ammo:

 #pragma strict
 
 var projectile : GameObject;
 var fireRate : float = 0.1;
 private var nextFire : float = 0.0;
 var hasMuzzleFlash : boolean = true;
 var muzzleFlash : GameObject;
 var bulletsLeft : int = 5;
 private var hasFired : boolean = false;
 private var bulletsPerShot : float = 1;
 
 
 function Update () {
     
     if(hasFired) {
         bulletsLeft -= bulletsPerShot;
 }
 
     
     if(Input.GetKey("space") && bulletsLeft <= 0) return;
     
     if(Input.GetButton("Fire1") && Time.time > nextFire && bulletsLeft > 0) {
         nextFire = Time.time + fireRate;
         var clone = Instantiate (projectile, transform.position, transform.rotation);
         hasFired = true;
     }

     
     if(Input.GetKey("space")) return;
     
     if(Input.GetButton("Fire1") && Time.time > nextFire) {
         nextFire = Time.time + fireRate;
         Instantiate (muzzleFlash, transform.position, transform.rotation); 
 }
 
 }



To make sure that the shooting stopped if ammo ran out, I made it return in that situation:

 if(Input.GetKey("space") && bulletsLeft <= 0) return;

Inside the firing script I made a boolean to make sure it actually fired:

 if(Input.GetButton("Fire1") && Time.time > nextFire && bulletsLeft > 0) {
             nextFire = Time.time + fireRate;
             var clone = Instantiate (projectile, transform.position, transform.rotation);
             hasFired = true;

And I allowed it to calculate the bullets:

 if(hasFired) {
             bulletsLeft -= bulletsPerShot;
     }

Well the only problem is, when I fire once, it never stops subtracting. It begins subtracting and subtracting and it never stops. SO i put it in a FixedUpdate() function (I wasn't sure how that would help, but it's worth a try), and that didn't change. So I put it in it's own function called function BulletCounter()but then it didn't work at all.

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
1
Best Answer

Answer by SilverTabby · Sep 24, 2011 at 12:51 AM

Your problem is that you are using "hasFired" in order to check if a bullet was fired this round. By itself it's OK, but the problem is that you never do this if a round was not fired:

      hasFired = false;

I would recommend the following to fix your repeat fire problem:

  • At the start of Update, hasFired = false;

  • Move "if(hasFired) " to the bottom of Update

Another thing I want to point out about this script:

from this if block (the final one in Update):

 if(Input.GetButton("Fire1") && Time.time > nextFire) {
         nextFire = Time.time + fireRate;
         Instantiate (muzzleFlash, transform.position, transform.rotation); 
 }

Move the Instantiate call to hasFired = true; block (the third one in Update); then delete the second if(Input.GetButton("Fire1")) block in update - it is unnecessary and redundant after moving the Instantiate call elsewhere

Comment
Add comment · Show 3 · 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 DayyanSisson · Sep 24, 2011 at 01:50 AM 0
Share

Perfect, it works.

avatar image SilverTabby · Sep 24, 2011 at 02:58 AM 0
Share

Remember to check an answer if it answered your question: it helps optimize the search system, declares a question as complete, and it gives me a nice little karma cookie :)

avatar image DayyanSisson · Sep 24, 2011 at 04:23 AM 0
Share

Sorry, I thought I did. (I knew that by the way)

avatar image
0

Answer by ocularcash · Sep 24, 2011 at 12:41 AM

i personally use

bulletsleft --;

if(bulletsleft < 0){

bulletsleft = 0;

}

if(bulletsleft == 0){

Reload();//if you have a reload function

}

}

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Issues with bullet not subtracting properly 1 Answer

How To Add A Limit To Amount Of Objects 1 Answer

How to avoid speed change in bullets while moving? 1 Answer

2D Bullet texture follow ray and Bullet Holes 1 Answer

How do I limit the speed of my car? 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