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 /
This question was closed Jul 13, 2018 at 08:58 PM by BluesyPompanno for the following reason:

Complete change of code

avatar image
0
Question by BluesyPompanno · Aug 06, 2017 at 06:40 PM · uigunreloadammoreloading

Gun - Ammo , reloading and UI problem.

Hello,I need help with realoding/shooting script and UI displaying.

When i press the reload button the displayed numbers are completely random than they should be.

For example the maxium number of ammo in the magazine is 10 and maximum ammo the player carries is 120 and when the reload function happens the ammo the player carries is completely random number

( 120 is maximum of ammo carried by the player -> Reaload ( set ammo in the magazine to 0 and add 10) -> the displayed number of maximum ammo carried by the player is 100,90,-80,70,20 and not 110)

The whole gun is controlled only through this script. I don't know how to fix this or how to change.

if anyone could help I would be most grateful. If any other tips about my script I would also be very happy :)

//variables// public float muzzleVelocity = 30f; public float msBetweanShots = 100f; public float Ammo = 30f; public float AmmoToReload = 30f; public float MaximumAmmo = 250f;

     //TIMES//
     public float ReloadTime = 2f;
     public float ShotDuration = 0.01f;
 
     float nextShotTime;
 
     //RECOIL//
     public Vector2 KickMinMax = new Vector2(1, 2);
     public Vector2 RecoilAngleMinMax = new Vector2(3, 5);
     public float recoilMoveSettleTime = .1f;
     public float recoilRotationSettleTime = .1f;
     Vector3 recoilSmoothDampVelocity;
     float RecoilAngle;
     float recoilRotSmoothDamVelocity;
 
     //BOOLEANS//
     public bool AUTOMATIC = false;
     private bool Reloading = false;
     private bool Shooting = false;
 
     //UI//
     public Text ammoTEXT;
     public Text ammoMAXIMUM;
     
 
     //SCRIPTS//
     playerSTATS plyrStats;
 
     private void Start()
     {
         ammoTEXT.text = Ammo.ToString();
         plyrStats = GetComponent<playerSTATS>();
         
     }
 
     public void Shoot()
     {
         if(Time.time > nextShotTime)
         {
             nextShotTime = Time.time + msBetweanShots / 1000;
             playerBULLET newBullet = Instantiate(BULLET, muzzle.position, muzzle.rotation) as playerBULLET;
             newBullet.SetSpeed(muzzleVelocity);
 
             Ammo -= 1;
 
             transform.localPosition -= Vector3.forward * Random.Range(KickMinMax.x, KickMinMax.y);
             RecoilAngle += Random.Range(RecoilAngleMinMax.x, RecoilAngleMinMax.y);
             RecoilAngle = Mathf.Clamp(RecoilAngle, 0, 30);
         }
     }
 
     private void Update()
     {
         //SHOOTING//
         if(AUTOMATIC == false)
         {
             if (Input.GetButtonDown("Fire1") && Ammo > 0)
             { Shooting = true; }
             else { Shooting = false; }
         }
         else if (AUTOMATIC == true)
         {
             if (Input.GetButton("Fire1") && Ammo > 0)
             { Shooting = true; }
             else {Shooting = false; }
         }
 
         if(Shooting == true)
         { Shoot();  }
 
         //RECOIL//
         transform.localPosition = Vector3.SmoothDamp(transform.localPosition, Vector3.zero, ref recoilSmoothDampVelocity, recoilMoveSettleTime);
         RecoilAngle = Mathf.SmoothDamp(RecoilAngle, 0, ref recoilRotSmoothDamVelocity, recoilRotationSettleTime);
         transform.localEulerAngles = Vector3.right * -RecoilAngle;
 
         //RELOADING//
         
         if (Input.GetKey(KeyCode.R))
         { Reloading = true; }
         else { Reloading = false; }
 
         if (Reloading == true)
         { StartCoroutine(RELOADING()); }
 
         ammoTEXT.text = Ammo.ToString();
         ammoMAXIMUM.text = MaximumAmmo.ToString();
     }
 
     private IEnumerator RELOADING()
     {
         Shooting = false;
         Ammo = 0;
 
         yield return new WaitForSeconds(ReloadTime - .25f);
         Ammo = +AmmoToReload;
         MaximumAmmo = MaximumAmmo - AmmoToReload;
 
 
         yield return new WaitForSeconds(.25f);
 
         Shooting = true;
         Reloading = false;
             
     }
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

1 Reply

  • Sort: 
avatar image
1

Answer by unidad2pete · Aug 06, 2017 at 08:19 PM

When you uses a Coroutine, after your yield return, your Update function start again, and other coroutine is started because Reloading its true. Im not sure if thats the problem, but try move your code checking if (Reloading == true) inside of your if(Input Key R) or add new bool set true on start of your coroutine and false at the end, and use for check if your coroutine are running before start another.

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

Follow this Question

Answers Answers and Comments

109 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS UI ammo display problem 0 Answers

Reload After Magazine Is Empty 2 Answers

reloading script problem 0 Answers

Reloading A Gun My Way 2 Answers

Reload after certain amount of shots? 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