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 ScripterToBe · Aug 03, 2014 at 09:35 PM · issue

Issue With Spare Ammo In Reload Script

Hey, I am new to scripting in Unity and decided to sit down and create some psuedocode for a reload script in C#. I started by listing variables that I believe would play an important role in a reloading script. Everything works just as I planned (to my surprise) however my issue is that when I click 'R' to reload, it subtracts a different number from my variable 'AmmoToReloadFrom' (spare ammo). My first time posting as well so don't be too harsh about any mistakes :) Thank you.

 public class ShootGun : MonoBehaviour {
 
 
     public Rigidbody bullet;
 
     public Transform bulletSpawn;
 
     public float bulletSpeed;
     public float AmmoToReloadFrom;
     public float MagSize;
     public float CurrentAmmo;
 
     public bool CanShoot = false;
 
 
 
 
     void Start () {
 
 
     }
     
 
     void Update () {
     
         if (Input.GetButton("Fire1") && CurrentAmmo > 0) {
 
             CanShoot = true;
 
             var bulletPrefab = (Rigidbody)Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
 
             bulletPrefab.rigidbody.AddForce(-transform.right * bulletSpeed);
 
             audio.Play();
 
             CurrentAmmo--;
         }
 
         if (Input.GetButton("Reload") && AmmoToReloadFrom > 0) {
 
             CurrentAmmo = MagSize;
             AmmoToReloadFrom = AmmoToReloadFrom - 20;
         }
     }
 
     void OnGUI() {
 
         if (CurrentAmmo <= 0 && AmmoToReloadFrom > 0) {
 
             GUI.Label(new Rect (Screen.width / 2 - 100, Screen.height / 2 - 100, 250, 200), "Press R To Reload");
         }
 
         if (AmmoToReloadFrom <= 0 && CurrentAmmo <= 0) {
 
             GUI.Label(new Rect (Screen.width / 2 - 100,Screen.height / 2 - 100, 250, 200), "No Ammo");
         }
     }
 }
Comment
Add comment · Show 4
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 meat5000 ♦ · Aug 03, 2014 at 09:04 PM 0
Share

Does $$anonymous$$agSize have a value?

What about bullets still in the mag?

avatar image ScripterToBe · Aug 03, 2014 at 09:17 PM 0
Share

Hi $$anonymous$$eat, forgot to mention that yes I assign values to every gun I attach this script to. In this current scenario, I assigned the FN SCAR model a $$anonymous$$agSize of 20. When you say bullets still in the mag I presume you mean the players current ammo which yes I have assigned to be 20. Thanks anyway. Just for heads up for others, the line that I noticed seems to be causing the issue is AmmoToReloadFrom = AmmoToReloadFrom - 20. To me this seems logical, however this does not seems to be the case.

avatar image meat5000 ♦ · Aug 03, 2014 at 09:33 PM 0
Share

Change

 Input.GetButton("Fire1")

to

 Input.GetButtonDown("Fire1")

It's just easier to see. It appears that you gun will be firing at a rate of FPS.

Also,

 if (Input.GetButton("Reload") && AmmoToReloadFrom > 0)
 {
     AmmoToReloadFrom = AmmoToReloadFrom - ($$anonymous$$agSize - CurrentAmmo);
     CurrentAmmo = $$anonymous$$agSize;
 }

This is what I mean by bullets left in clip :)

avatar image ScripterToBe · Aug 03, 2014 at 10:02 PM 0
Share

Ahh I get you. Thanks you so much man! Works perfectly :D Have a good day! Oh by the way, this was the first script that I sat down and came up with from general knowledge - not much but would you have any more constructive criticism at all?

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Turrets |Aiming don't work 1 Answer

Want to optimize this code. causes a wired issue 1 Answer

Everything is black except for the scene 1 Answer

iOS Bulding Error ( EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit=2048 MB, unused=0x0)) Xcode 12 0 Answers

Dude, where's my scene? 2 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