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 Crumpet · May 26, 2014 at 11:22 AM · gunshootbullets

My reloading script not working after low bullets

My firing script for my gun is working up until totalnumberofbullets (this should be the complete total number of bullets excluding the ones in the clip) becomes lower than 7 (my full amount in my clip). I think I could fix this with a bit of messing around however when (for example) I press reload when the total number of bullets is 2 and I have 3 totalnumberofbullets (total of bullets all together) left it will make my bullets (the amount of bullets that is in the clip of the gun) 7 and the totalnumberofbullets will end up being -4. If this is hard to understand maybe try copying my script into unity and testing it. I'm not sure how I can go about fixing this and it seems to be one of the final issues for my gun script. Here's the code for it (I am still pretty new to programming so the script is probably pretty bad)

 using UnityEngine;
 using System.Collections;
 
 public class wfa3 : MonoBehaviour 
 {
     public static float bullets = 7f;
     public GameObject deagle;
     public bool animationreloadplayed = false;
     public bool fired = false;
     public float Distance;
     public float MaxDistance = 100;
     public static float Damage = 25;
     public bool timedown;
     public float time = .84f;
     public bool firedshot = false;
     public static bool haswaited = false;
     public static bool haswaited2 = false;
     public bool Reloadinggun = false;
     public static bool m1haspressed = false;
     public bool firinganimationisplaying = true;
     public static bool firinganimationisplayingplayed = false;
     public bool Firing = true;
     public float totalnumberofbullets = 28f;
     public bool reloadable = true;
     public float howmuchtotake = 0;
     public bool taken = false;
     public int TheSize = 100;
     public float GUIPosition1;
     public float GUIPosition2;
     public float numberofbulletstoload = 0f;
 
     void Start ()
     {
         
     }
 
     void Update ()
     {
         if (totalnumberofbullets <= 0f)
         {
             reloadable = false;
         }
         if (Input.GetButtonDown ("Fire1") && Reloadinggun == false && m1haspressed == false && bullets > 0f)
         {
             firedshot = true;
             fired = true;
         }
 
         if (bullets <= 0f && bullets < 7 && Firing == false && reloadable == true)
         {
             animationreloadplayed = true;
             Force2.IsTiming2 = true;
             totalnumberofbullets = totalnumberofbullets - 7;
         }
         if (fired == true)
         {
             RaycastHit hit;
             {
                 if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.down), out hit) && haswaited == true)
                 {
                     Distance = hit.distance;
                     if (Distance < MaxDistance)
                     {
                         hit.transform.SendMessage ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
                         Debug.DrawLine(transform.position, hit.point, Color.red);
                         fired = false;
                     }
                 }
             }
         }
         if (Input.GetKeyDown (KeyCode.R) && bullets <= 6 && reloadable == true && Firing == false && totalnumberofbullets >= 7)
         {
             animationreloadplayed = true;
             Force2.IsTiming2 = true;
             howmuchtotake = 7 - bullets;
             totalnumberofbullets = totalnumberofbullets - howmuchtotake;
         }
         if (animationreloadplayed == true)
         {
             Debug.Log ("Reloading");
             deagle.animation.Play ("Reload");
             bullets = 7f;
             Force2.IsTiming2 = false;
             if (bullets == 7 && animationreloadplayed == true)
             {
                 animationreloadplayed = false;
             }
         }
         if (firedshot == true && animationreloadplayed == false)
         {
             Debug.Log("Fired Shot!");
             Force.IsTiming = true;
             if (haswaited == true)
             {
                 bullets = bullets -1f;
                 deagle.animation.Play ("Fire");
                 firedshot = false;
                 audio.Play ();
                 haswaited = false;
                 firinganimationisplayingplayed = false;
                 Debug.Log("I have waited!");
             }
         }
         if (totalnumberofbullets <= 7 && Input.GetKeyDown (KeyCode.R) && bullets <= 6 && reloadable == true && Firing == false)
         {
             animationreloadplayed = true;
             Force2.IsTiming2 = true;
             numberofbulletstoload = 7-bullets;
             totalnumberofbullets = totalnumberofbullets - numberofbulletstoload;
             totalnumberofbullets = bullets;
             if (bullets == 7 && animationreloadplayed == true)
             {
                 animationreloadplayed = false;
             }
         }
         if (!animation.IsPlaying("Reload"))
         {
             Reloadinggun = false;
         }
         else
         {
             Reloadinggun = true;
         }
         if (Force.TotalTime >= 0)
         {
             Firing = true;
         }
         if (haswaited == true)
         {
             Firing = false;
         }
     }
 
         void OnGUI ()
         {
         GUI.skin.label.fontSize = TheSize;
         GUI.Label (new Rect (GUIPosition1, GUIPosition2, 450, 100), "  /"+(totalnumberofbullets));
         GUI.Label (new Rect (GUIPosition1, GUIPosition2, 450, 100), ""+(bullets));
         } 
     }
 
 
Comment
Add comment · Show 2
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 Crumpet · May 26, 2014 at 10:02 AM 0
Share

I know this question is pretty hard to understand so I'm uploading a video to youtube if it's necessary. This will be a while though.

avatar image Crumpet · May 26, 2014 at 10:24 AM 1
Share

https://www.youtube.com/watch?v=uSC3QLeqthE&feature=youtu.be

Video is done :D

2 Replies

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

Answer by Andres-Fernandez · May 27, 2014 at 07:12 AM

If I were you, I would get the number of bullets and totalnumberofbullets like this:

    float bulletsWanted = 7-bullets;
    float totalNumberOfBulletsBeforeLoad = totalnumberofbullets; // This will help us later
    totalnumberofbullets = Mathf.Max(totalnumberofbullets - bulletsWanted, 0); // Never gets below 0
    float actualNumberOfBulletsToLoad = totalNumberOfBulletsBeforeLoad - totalnumberofbullets; // We actually get the difference between the amount of total bullets before reloading and after reloading
    bullets += actualNumberOfBulletsToLoad;
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
avatar image
0

Answer by Crumpet · May 27, 2014 at 09:30 AM

Thank you very much Andres Fernandez that worked for me. :D Thanks for your help too HarshadK I appreciate it. I'm not really sure how the Mathf.Max works though, it just gets the highest of two values? So therefore it would be impossible to get any lower than 0. This is hard to understand when the code is in place of the numbers. :/

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 Andres-Fernandez · May 27, 2014 at 09:42 AM 0
Share

No problem. That's exactly what it does. Check the docs:

https://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.$$anonymous$$ax.html

The line:

 totalnumberofbullets = $$anonymous$$athf.$$anonymous$$ax(totalnumberofbullets - bulletsWanted, 0);

is just a "fast" way of checking if you have more bullets than you want to load, and substract them from your pool of bullets. You could change the script for something like this:

 if (totalnumberofbullets >= bulletswanted) {
    totalnumberofbullets = totalnumberofbullets - bulletswanted;
    numberofbulletstoload = bulletsWanted;
 } else {
    numberofbulletstoload = totalnumberofbullets;
    totalnumberofbullets = 0;
 }

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

21 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

Related Questions

Trouble with script to fire a bullet from a handgun 1 Answer

Troubles With A Shoot Script 1 Answer

Raycasting to the middle of the screen? 2 Answers

Shoot only once when clicked 1 Answer

Having Trouble Shooting 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