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 /
  • Help Room /
avatar image
0
Question by Dragonfly3r · Nov 29, 2015 at 12:26 AM · c#timegunreloadreloading

Gun not reloading properly & time not deducting correctly when below 10 seconds

So I'm working on my 3rd prototype section for my mechanics for my game idea and currently working on a gun that raycasts a bullet hole when it fires at an object however if the object is not the right prop (The game is based around hunters and props. The hunters shoot the props to win the game) then it will deduct 10 seconds off of the game timer. This is currently working however when the seconds on the game timer are below 10 seconds e.g. 01:06

If the hunter shoots an incorrect prop the timer should next say 00:56 however instead it simply resets the 59 back to 0 now I know that is referring to the top of my update method in which a if statement detects if the seconds is less than or equal to 0 it will then set it to 59 seconds however I don't know how to make it take into account the previous time penalty so that displays those extra lost seconds from incorrectly hitting the wrong prop.

My other bugs I have is during the reloading of the gun currently at the moment I have it working for if you shoot 5 bullets out of a clip of 30 and reload it will set your ammo back to 30 and subtract 5 from the total ammo. However if you decide to empty an entire clip of ammo of 30 and then reload via the automatic reload then it will subtract 5 bullets from the total ammo before then firing the remaining 30 bullets in the full clip which then leads onto my final bug in which bullets in clip = 0 and the total ammo = 0 then for some reason it will reload 30 bullets from out of nowhere and then place the total ammo count into the negative.

I have included videos of each bug in action. and is posted below.

My code is as follows:

 public class BulletHoles : MonoBehaviour 
 {
     public GameObject t_bullet;
     public GameObject prop;
     private float nextFire = 0.0f;
     private float fireRate = 0.5f;
     //public AudioClip gunShot;
     //public AudioClip reload;
     public int bp_Clip = 30;
     public int t_Ammo = 90;
     public int r_Ammo = 30;
     public int tmp_Ammo = 0;
     
     public float t_Left;
     public Text timerText;
     public Text timer;
     public Text m_Winner;
     public float min = 9.0f;
     public float sec = 59.0f;
     public float maxSec = 59.0f;
     public float tmp_Sec = 0.0f;
     public float tmp_SecTwo = 0.0f;
     public float delay_Sec = 10.0f;
 
     public Text ammo;
 
     // Update is called once per frame
     void Update()
     {
         if (sec <= 0)
         {
             sec = 59.0f;
             if (min >= 1)
             {
                 min--;
             }
             else
             {
                 min = 0;
                 sec = 0;
                 m_Winner.text = "Props Win";               
             }
         }
         else
         {
             sec -= Time.deltaTime;
         }
 
         timerText.text = "Time Left: ";
         timer.text = min.ToString("00") + ":" + sec.ToString("00");
 
         ammo.text = bp_Clip.ToString("00") + "/" + t_Ammo.ToString("00");
 
         if (bp_Clip > 30)
         {
             bp_Clip = 30;
         }
         if (t_Ammo == 0)
         {
             t_Ammo = 0;
         }
 
         Fire();
         if (Input.GetKeyDown(KeyCode.R)) // this 1 works
         {
             if (bp_Clip <30)
             {
                 StartCoroutine(Reload());
             }
         }
         if (bp_Clip < 1) // this 1 subtracts ammo from total ammo for 2-3 bullets before
             // subtracting from clip
         {
             StartCoroutine(Reload());
         }
                
     }
   
     void Fire()
     {
         if (bp_Clip > 0)
         {
             if (Input.GetButton("Fire1") && Time.time > nextFire)
             {
                 nextFire = Time.time + fireRate;
                 //      AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
                 ForceFire();
                 bp_Clip -= 1;
             }
         }
     }
     // Now reloads properly however if total ammo is at 0 it still takes extra ammo and pushes the total in
     // to negative figures
     // also if clip is full empty and then reloads it takes off some ammo from the total before then taking out of the
     // clip
     IEnumerator Reload()
     {
         Debug.Log("Reload Two");
         //AudioSource.PlayClipAtPoint(reload, transform.position, 1);
         yield return new WaitForSeconds(3.0f);
         tmp_Ammo = r_Ammo - bp_Clip;
         t_Ammo = t_Ammo - tmp_Ammo;
         bp_Clip = bp_Clip + tmp_Ammo;
         
     }
 
     void ForceFire () 
     {
         Vector3 fwd = transform.TransformDirection(Vector3.forward);       
 
         RaycastHit hit;
 
         Debug.DrawRay(transform.position, fwd * 10, Color.green);
 
         if (Input.GetButton("Fire1"))
         {
             if (Physics.Raycast(transform.position, fwd, out hit))
             {
                 Instantiate(t_bullet, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
 
                 if (hit.collider.tag == "Prop")
                 {
                     m_Winner.text = "Hunters Win";
                     Time.timeScale = 0;   //Level Timer is stopped at whatever time it is at.
 
                 }
                 else if (hit.collider.tag != "Prop") // If hunter hits incorrect prop, subtract 10 seconds from level timer.
                 {
                     sec -= 10.0f;
 
                     if (sec < 10.0f && min > 1)
                     {
                         tmp_Sec = maxSec - sec;
                         sec = 0.0f;
                         sec = sec - tmp_Sec;
                     }
                   
                      //   tmp_Sec = maxSec - sec;
                        // tmp_SecTwo = maxSec - tmp_Sec;
                         //sec = sec - tmp_SecTwo;
                         //min -= 1;
                  //       sec = sec - tmp_Sec;
                 //       // if (sec <= 59.0f)
                        // {
                         //    sec = sec - tmp_Sec;
                       //  }
                     
                     if (sec < 10.0f && min < 1) //This part works and makes it so time doesn't go into negative. If shot incorrect prop.
                     {
                         sec = 0.0f;
                     }
                     // Current bug - If you shoot the incorrect prop when the timer is x:09 or any other second value under 10 it will not take into account
                     // the extra seconds being deducted when the second timer resets e.g.  9:05 - 10 seconds should equal 8:55 however it displays 8:59
                 }
             }
         }
     }
 }

Timer bug: https://youtu.be/X5ltaCDjZp0

Reload bug 1: https://youtu.be/JjN95yo9lAw

Reload bug 2: https://youtu.be/GMvyizUxslg

Anyone know what could be the solutions to the above problems?

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
1
Best Answer

Answer by unnamed1334 · Jan 09, 2016 at 07:56 PM

I had a bit of trouble figuring out what the variables were but I think I found the bugs.

Line 58. This if statement does nothing. It checks to see if t_Ammo is zero and, if so, set it to zero.


Reload bugs:

Line 63 & 71. Both of these trigger the reload but neither check to see id there is enough ammo or if the gun is already reloading(This makes it look like you are shooting ammo directly from the unload ammo pool right after reloading. If you reload because you shot all the bullets you end up reloading every frame for 3 seconds after the first reload ends). And Line 96. This doesn't check to see if there is any ammo left before reloading, causing t_Ammo to drop into negative values.

Here is what I thing is happening:

Line 101. The amount of ammo that is going to be reloaded is calculated. This is not taking into account remaining ammo, so you might need a if statement if the amount of ammo needed is greater then remaining ammo.

Line 102. The amount of unloaded ammo is reduced by the amount of needed ammo. This drops the unloaded ammo into a negative if it is to low.


Timer Bug:

Line 32. Change this from

sec = 59.0f;

to

sec += 59.0f;

So if sec drops far below 0 (because the player hits something that is not a prop). The extra time that was removed is preserved.

Line 131. One source of the timer bug. I am not sure why there is a if statement here. You are effectively doing:

sec = sec - maxSec - sec;

Which is the same as:

sec = maxSec;

If the minute number is 2 or greater and timer was initial less then 20 (less then 10 after subtracting 10).

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 Dragonfly3r · Jan 10, 2016 at 12:21 AM

@unnamed1334 I should of posted update to this I figured out the timer bug on my own.

What I ended up doing was setting up 2 temp values and a bool that is set to true within a if statement

which would check if the sec value was under 10

Then it would subtract the current time from the maxSec variable and store the difference value as the 1st temp float variable

So for example the times is 01:06 the difference between 6 & 10 (maxSec) is equal to 4 so the 1st temp value = 4

Next it then finds out the difference between the 1st temp value and the maxSec value to find the the value for 2nd temp value.

In the example the 1st temp value = 4 so the difference between 4 & 10 (maxSec) equals 6.

Then within the update method I simply check if the seconds was below 10 and the bool was set to true

After which it would take the 1st temp value from the current time. Reset the timer back to 59.9 seconds and then take away the 2nd temp value from the 59.9 seconds thus fixing the bug.

Timer Method check by update method

 else if (sec < 10 && delay_Bool == true) // If the seconds counter is below 10 seconds and the delay bool has become true.
         {
             sec = sec - tmp_Sec; // It will subtract the first tmp value
             sec = 59.9f; //Set the timer to normal
 
             sec = sec - tmp_SecTwo; // Substract 2nd temp value
             delay_Bool = false; // Set delay bool back to false until it is true again following above conditions met
 
             Min();
         }


ForceFire method fix

   else if (hit.collider.tag != "Prop") // If hunter hits incorrect prop, subtract 10 seconds from level timer.
                 {
                     if (sec < 10.0f)
                     {
                         delay_Bool = true;  
                         tmp_Sec = delay_Sec - sec; // Calculates the difference between 10 seconds and current state of sec timer to make temp value e.g.  sec = 4 tmp_Sec will = 6
                         tmp_SecTwo = delay_Sec - tmp_Sec; // Calculate the difference between 10 seconds and the 1st temp sec value to find the 2nd value e.g. tmp_Sec = 6, so tmp_SecTwo = 4                      
                     }
                     else if (sec < 10.0f && min < 1) //This part works and makes it so time doesn't go into negative. If shot incorrect prop.
                     {
                         sec = 0.0f;
                     }
 
                     else
                     {                  
                         sec -= 10.0f;
                     }

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

34 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 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

GAME - profit simulating after game is close - ANDROID 0 Answers

Wait time after coroutine's wait seconds is complete 0 Answers

why does timescale not work? 2 Answers

Load score 0 Answers

2D Platformer Gun Equip Script Not Working 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