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 4u2fap · Mar 05, 2014 at 08:04 PM · javascriptweaponreloading

weapon reload malfuction

i made a shotgun script witch is working really good until i noticed that for some reason after i reload instead of using the bullet from my clip it use the bullets from my total ammunitions for about a seconds and then it start using the one from my clip so i looked at where i could of went wrong but i cant find it so any idea on how to solve this?

relevant part of the script :

        function Update(){
         if(Input.GetButton("Fire1") && allowFire && Time.time > nextFire){
         
             var hit : RaycastHit;
             curClip --;
             nextFire = Time.time + fireRate;
             
             PlayerProperty.AgroLvl = gunAgro;
             audio.PlayOneShot(gunSound);
             spreading = true;
             spread += spreadPerSecond;
                 }
     if(Input.GetButtonDown("r") && ammunition > 0 && curClip < maxClip){
             Reload();
             allowFire = false;
             spreading = false;
         }
         
         if(curClip <= 0 && ammunition > 0){
             allowFire = false;
             spreading = false;
             Reload();
         }
         else if(curClip <=0 && ammunition <= 0){
             allowFire = false;
             spreading = false;
         }
         if (count >= pellet){
             count = 0;
         }
     }
  
 function Reload(){
         PlayerProperty.AgroLvl = baseAgro;
         yield WaitForSeconds(reload);
         if( maxClip <= ammunition && curClip <= 0){
             ammunition = ammunition - maxClip;
             curClip = maxClip;
             allowFire = true;
         }
         if(maxClip > ammunition && curClip <= 0){
             curClip = ammunition; 
             ammunition = 0;
             allowFire = true;
         }
         if( maxClip <= ammunition && curClip > 0){
             ammunition = ammunition - (maxClip - curClip);
             curClip = maxClip;
             allowFire = true;
         }
         if( (maxClip - curClip) >= ammunition && curClip > 0){
             curClip = curClip + ammunition;
             ammunition = 0;
             allowFire = true;
         }
         if( (maxClip - curClip ) < ammunition && curClip > 0){
             ammunition = ammunition - (maxClip - curClip);
             curClip = maxClip;
             allowFire = true;
         }
     
 }
  
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
0
Best Answer

Answer by perchik · Mar 05, 2014 at 08:51 PM

Your logic on reload is very confusing...lots of cases that seem to overlap...why not do something like this:

 function Reload(){
     yield WaitForSeconds(reload);  

     var bulletsNeeded :int = maxClip - curClip; //#bullets needed to fill my clip
     var bulletsGot: int =0; 
     
     if(bulletsNeeded < ammunition) //there's enough bullets to give what I need to reload
     { 
         ammunition = ammunition - bulletsNeeded; //take those bullets from ammo
         bulletsGot = bulletsNeeded; //keep up with how many you took
     }
     else{ //theres not enough (may be 0, who cares)
         bulletsGot= ammunition; //take everything we have left
         ammunition = 0; //set ammunition to empty
     }
     curClip = bulletsGot; //put however many bullets we got into the clip
     
     if(bulletsGot==0)//if we didn't get anything, means we were out of ammuntion
         allowFire=false; 
     else
         allowFire =true;
 }
   

Note this is untested code. This covers all the cases but the logic seems a lot clearer to me.

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 4u2fap · Mar 05, 2014 at 08:57 PM 0
Share

well even if it does not work you gave me a general idea on how to do it thank you alot

avatar image
0

Answer by Dblfstr · Mar 05, 2014 at 08:10 PM

I thinkit is because you Reload() which has a wait for seconds. Then you set allowFire adn spreading to false. Try rversing them, like in the next if statement.

  if(Input.GetButtonDown("r") && ammunition > 0 && curClip < maxClip){
 
 allowFire = false;
 spreading = false;
 Reload();
 }
Comment
Add comment · Show 2 · 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 4u2fap · Mar 05, 2014 at 08:33 PM 0
Share

the problem is not when he reload but after it does what i mean is that the reload timer work but as soon as the clip is full ins$$anonymous$$d of shooting its clip it shoot its ammunitions for about 1 sec

avatar image Dblfstr · Mar 05, 2014 at 08:44 PM 0
Share

I see. I cannot tell where, in this code, that the ammunition variable is accessible other than in the reload function. Except that you allow firing while reloading. This could cause what you see. When actually the bullet IS co$$anonymous$$g from your clip, but the Reload function is filling it right back up, so you see the ammunition go down, and not the clip.

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

22 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

Related Questions

Deactivate children with .active = false 2 Answers

Controlling Weapons With MouseWheel 2 Answers

weapon sweep 1 Answer

Weapon Switching 2 Answers

How do I add ammo and reloads? 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