- Home /
Complete change of code
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;
}
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.
Follow this Question
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