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
1
Question by Borzi · Apr 30, 2013 at 07:51 PM · fpsshootingweaponreloading

Reloading Script

I am creating a projectile shooting script. I have no compile errors, but I encountered some problems after I added the reloading part. The gun will not fire anymore and I was wondering if some of you know what the problem is. Thank you in advance (btw Im using a seperate script on a bullet projectile that is spawned using this script).

 var MaxAmmo =  1;
 var Ammo = MaxAmmo;
 var Bullets = 15;
 
 private var timer = 0.0;
 var reloadTime = 10.0;
  
 var Fire : String = "MusketFire";
  
 function Start () 
 {
  
 }
  
 function Update () 
 {
     if(Fire == "MusketFire")
     {
        if(Ammo > 0)
        {
          if(Input.GetMouseButtonDown(0))
          {
          FireOneBullet();
          }
        }
     }
  
  
     if(Input.GetKey("r"))
     {
        Reload();
    
     }
 }
  
 function FireOneBullet ()
 {
     var Bullet = Instantiate(Bullet, FirePoint.transform.position, transform.rotation);
     Ammo --;
 }
  
 function Reload ()
  
 {
     if(Ammo > MaxAmmo && Bullets > 0)
     { 
        if(timer<reloadTime)
        {
          timer+=Time.deltaTime;
        }
        else
          timer=0.0; 
  
     }
     if(timer>=reloadTime)
         {
                Ammo ++;
                Bullets --;
         } 
 }
Comment
Add comment · Show 18
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 Borzi · May 01, 2013 at 07:28 PM 0
Share

Any ideas?

avatar image ExTheSea · May 01, 2013 at 07:38 PM 0
Share

To do some debugging you could change the first if-clauses in your update function to something like this:

 if(Input.Get$$anonymous$$ouseButtonDown(0))
 {
  Debug.Log(Fire);
  if(Fire == "$$anonymous$$usketFire")
   {
   Debug.Log(Ammo);
   if(Ammo > 0)
   {
    Debug.Log("Fire Bullet");
    FireOneBullet();
   }
  }
 }

Then you can see if it has to do with any of those variables.

avatar image ExTheSea · May 02, 2013 at 05:35 PM 0
Share

Did you try the debug logging? Does the FireOneBullet function get called?

avatar image Borzi · May 03, 2013 at 10:08 AM 0
Share

No it does not :( Fire is, but "FireOneBullet isn't

avatar image ExTheSea · May 03, 2013 at 03:41 PM 0
Share

So Fire gets logged but Fire Bullet doesn't. Does Ammo get called and what gets logged?

Show more comments

2 Replies

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

Answer by Fornoreason1000 · May 01, 2013 at 07:37 PM

at line 45, your using a classic wrong arrow typo. when is Ammo ever more than Max Ammo? your timer never starts, and never finishes, also you never reset your timer after the actual reloading, so the user has to press R twice the next time around. the largest error is that your timer is called only one per frame so you need to hold down are for 10 seconds before reloading will work.

got it working though:

 var MaxAmmo =  1;
 var Ammo = MaxAmmo;
 var Bullets = 15;
 
 var Bullet : GameObject;
 var FirePoint : GameObject;
  
 private var timer = 0.0;
 var reloadTime = 10.0;
  
 var Fire : String = "MusketFire";
 var reloading : boolean;
  
 function Start () 
 {
  
 }
  
 function Update () 
 {
     if(Fire == "MusketFire")
     {
         if(Ammo > 0)
         {
             if(Input.GetMouseButtonDown(0))
             {
                 FireOneBullet();
             }
         }
     }
  
  
     if(Input.GetKey("r"))
     {
         Reload();
  
     }
     if(reloading) {
         if(timer<reloadTime)
         {
             timer+=Time.deltaTime;
         }
         else
             timer=0.0; 
  
         if(timer>=reloadTime)
         {
             Ammo ++;
             Bullets --;
             timer=0.0;
             reloading = false;
         } 
     }
 }
  
 function FireOneBullet ()
 {
     var Bullet = Instantiate(Bullet, FirePoint.transform.position, transform.rotation);
     Ammo --;
 }
  
 function Reload ()
  
 {
     if(Ammo < MaxAmmo && Bullets > 0)
     { 
         reloading = true;
     }
 
 
     
 }

though 10 seconds for a reload time is pretty excessive. hope it helps

Comment
Add comment · Show 9 · 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 fafase · May 01, 2013 at 07:52 PM 1
Share

"you need to press R hundreds of times", actually not really since he uses Get$$anonymous$$ey, he would have to keep pressing non-stop.

avatar image Fornoreason1000 · May 01, 2013 at 07:54 PM 0
Share

I stand corrected, i just saw "Input" then skipped past it

avatar image Borzi · May 02, 2013 at 03:43 PM 0
Share

Okay thanks for the help guys I appreciate you taking the time to help a noob out. Unfortunately the bullet still doesn't instantiate even after I tried your script. On the other hand, the reloading works perfectly!

avatar image Fornoreason1000 · May 02, 2013 at 09:19 PM 0
Share

did you assign the bullet? did you assign Firepoint? if not there's your problem, if there are and still no resuts. try Firepoint.transform.rotation the ammo decreases so your Instantiate must be at fault

avatar image Borzi · May 03, 2013 at 10:40 AM 0
Share

Yes they are. Im not quite sure if I get what you mean with that could you demonstrate what you want to do in a small script?

Show more comments
avatar image
2

Answer by fafase · May 01, 2013 at 07:41 PM

I think this is wrong:

 function Reload (){
   // This won't happen since you have Ammo = MaxAmmo at the top
   if(Ammo > MaxAmmo && Bullets > 0) 
   {
     if(timer<reloadTime)
     {
         timer+=Time.deltaTime;
     }
     else
         timer=0.0;     
   }
   if(timer>=reloadTime)
   {
     Ammo ++;
     Bullets --;
   }
 }

timer is not increasing since you would have to hold the button down.

I think you are willing to use coroutine as such:

 function Reload (){
   if(Bullets > 0) // Not sure what you are checking here though
   {
     while(timer<reloadTime)
     {
         timer+=Time.deltaTime;
         yield;
     }
     timer=0.0;     
     Ammo ++;
     Bullets --;
   }
 }
Comment
Add comment · Show 1 · 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 Borzi · May 02, 2013 at 03:48 PM 0
Share

Unfortunately this didn't solve my problem, but thank you for pointing out the mistake! Since in my game, the player is using a musket, bullets is equal to magazines in a normal first person shooter. When your out of bullets, you can no longer reload the gun, basically.

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

15 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

Related Questions

Reload Ammo is not working 0 Answers

Reload Ammo is not working 1 Answer

unlocking weapon (fps) 1 Answer

Wrong Shoot Animation 0 Answers

Ultimate FPS not working correctly. 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