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 cgartist4jc · Jun 24, 2011 at 04:14 PM · variablegunreloadreloadingsubtracting

Reloading A Gun My Way

Hi Yes, I know there are several topics like this, but I think that I should be able to load a gun this way. I think its just that I don't know how to tell the computer to do it. Can you help?

 var MPS : int = 35; //How fast the bullet goes
 var BB : Rigidbody; 
 var automaticWeapon : boolean = true;
 var semiAutoWeapon : boolean = false;
 private var lastShot : float;
 var shotInterval : float = .1;
 var deleteBulletTime : int = 3;
 var magSize : int = 15;//Clip Size

 function Update () {
     if(semiAutoWeapon == true && automaticWeapon == true){
         Debug.Log('Houston, We have a Problem!');//My gun can't be BOTH Semi and Auto
     }
     if(semiAutoWeapon == true && automaticWeapon == false){//Semi-Auto Settings
         if(Input.GetButtonDown('Fire1')){
             if(magSize > 0){//If magSize isn't empty, Fire Away!
                 var instantiatedBB : Rigidbody = Instantiate(BB,
                         transform.position, transform.rotation );
                     instantiatedBB.velocity = transform.TransformDirection(
                         Vector3( 0, 0, MPS ));
                 var magSize = magSize - 1; //What i want is that every
                          time you shoot, the var magSize lessens by one. 
             }
             else{ //If magSize is empty (not greater then 0), reload!
                 Reload();
             }
         }
     }
     if(automaticWeapon == true && semiAutoWeapon == false){//Auto Settings
         if(Input.GetMouseButton(0)){
             if(Time.time - lastShot >= shotInterval){
                 var instantiatedTwoBB : Rigidbody = Instantiate(BB,
                          transform.position, transform.rotation);
                 instantiatedTwoBB.velocity =
                          transform.TransformDirection(Vector3( 0, 0, 50 ));
                 lastShot = Time.time;
             }
         }
     }
 }
 function Reload (){ //I'm just testing to see if I can get to the Reload stage...
     Debug.Log('Oops, Out of Ammo!');
 }

My main problem is subtracting from the varible. Thank You for all the help!

Comment
Add comment · Show 7
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 Marnix · Jun 24, 2011 at 04:26 PM 0
Share

Could you please reformat your code? This is unreadable. Add a TAB by adding four SPACEs

avatar image Joshua · Jun 24, 2011 at 04:31 PM 0
Share

It also seems you forgot to check if you have ammo when you're in 'automatic' mode, and the bullet velocity is not equal to $$anonymous$$PS for some reason.

avatar image Chris D · Jun 24, 2011 at 04:55 PM 0
Share

formatted.

avatar image Waz · Jun 24, 2011 at 09:58 PM 0
Share

Read about enum ... having two mutually exclusive booleans is odd.

avatar image burnumd · Jun 24, 2011 at 10:05 PM 0
Share

An enum would be a start. It's worth considering that this kind of situation is exactly what polymorphism is for, as a way of making this code more flexible.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by burnumd · Jun 24, 2011 at 04:21 PM

This problem stems from the fact that you're re-declaring your magSize variable locally within update:

 var magSize = magSize - 1;

should be

 magSize = magSize - 1;

or even be fancy and use

 magSize--;

You'd be well served doing some research on object-oriented design. Using booleans to determine the type of gun is liable to lead to all sorts of errors. You even attempt to catch a design-time error with the automatic/semi-automatic flags that could be abrogated by subclassing different types of guns (or even using an enum rather than boolean flags).

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 cgartist4jc · Jun 24, 2011 at 04:26 PM 0
Share

O$$anonymous$$... Can You tell me where I can get started with object oriented design? I kinda a total newbie at this.
PS - Thanks for the script help! That worked wonders!

avatar image burnumd · Jun 24, 2011 at 04:33 PM 1
Share

In terms of free resources, Wikipedia is generally a decent jumping-off point for program$$anonymous$$g topics (when you've digested them, be sure to check out the source/external links). The OOD page is here: http://en.wikipedia.org/wiki/Object-oriented_design If you're interested in books, I'm personally fond of the Headfirst series: http://headfirstlabs.com/books/hfooad/ which are very beginner friendly.

avatar image
0

Answer by Byterunner · Jun 24, 2011 at 04:29 PM

Your problem is with scope. You're redeclaring the variable magSize as a new variable locally within your if block and thus not affecting the global variable. Here's a good start to understanding scope: http://javascript.about.com/library/bltut07.htm

Also make sure that your automaticWeapon and semiAutomaticWeapon booleans are set properly - as it's written currently you'll only get the reload message when your weapon is set to semiAutoWeapon.

Try this:

 var MPS : int = 35; //How fast the bullet goes
 var BB : Rigidbody; 
 var automaticWeapon : boolean = true;
 var semiAutoWeapon : boolean = false;
 private var lastShot : float;
 var shotInterval : float = .1;
 var deleteBulletTime : int = 3;
 var magSize : int = 15;//Clip Size
 
 function Update () {
     if(semiAutoWeapon == true && automaticWeapon == true){
         Debug.Log('Houston, We have a Problem!');//My gun can't be BOTH Semi and Auto
     }
     if(semiAutoWeapon == true && automaticWeapon == false){//Semi-Auto Settings
         if(Input.GetButtonDown('Fire1')){
             if(magSize > 0){//If magSize isn't empty, Fire Away!
                 var instantiatedBB : Rigidbody = Instantiate(BB, transform.position, transform.rotation );
                 instantiatedBB.velocity = transform.TransformDirection(Vector3( 0, 0, MPS ));
                 magSize--; //What i want is that every time you shoot, the var magSize lessens by one. 
             }
             else{ //If magSize is empty (not greater then 0), reload!
                 Reload();
             }
         }
     }
     if(automaticWeapon == true && semiAutoWeapon == false){//Auto Settings
         if(Input.GetMouseButton(0)){
             if(Time.time - lastShot >= shotInterval){
                 var instantiatedTwoBB : Rigidbody = Instantiate(BB, transform.position, transform.rotation);
                 instantiatedTwoBB.velocity = transform.TransformDirection(Vector3( 0, 0, 50 ));
                 lastShot = Time.time;
                 magSize--; //What i want is that every time you shoot, the var magSize lessens by one. 
             }
         }
     }
 }
 function Reload (){ //I'm just testing to see if I can get to the Reload stage...
     Debug.Log('Oops, Out of Ammo!');
 }


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

10 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

Related Questions

Reload after certain amount of shots? 1 Answer

Gun - Ammo , reloading and UI problem. 1 Answer

Pass a JS Script to C#? 1 Answer

realistic reload system? 1 Answer

Gun reloading script/instructions? 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