- Home /
How do I get Single ammo reloading to work?
I'm trying to make my gun script reload similar to that of current FPS in which if a player shoots 1 bullet and the player reloads the gun then it will reload the gun to the full clip amount and take into account of the 1 ammo left so that on the final clip of ammo there'd be 1 ammo less than on a normal clip.
For example you have 35 ammo with 7 per clip. You shoot say 2 times and then reload meaning your total ammo is 33. You will have a full clip until your last clip where there will be 5 ammo in the clip instead of the full 7 due to the already spent ammo.
I've managed to make it so my gun reloads after all 7 ammo per clip has been used up and then reloads with another 7 ammo however I have been unable to figure out how to implement the above problem. Anyone able to help me out to figuring out what I need to do, to do this?
I've implemented below the use of the R button on the keyboard for when you will reload for single shot ammo however when I currently reload it just slows the audio for reloading so it sounds all distorted. And also when bulletsLeft is = to less than 0 in the clip then it will automatically reload a fresh new clip of 7 ammo.
RayShoot Script
public int clip = 80;
public int bulletsPerClip = 60;
public int bulletsLeft = 0;
public int particleSpeed = 200000;
public float damage = 10.0f;
public float range = 1000.0f;
public float reloadTime = 1.6f;
public float force = 1000;
public float fireSpeed = 15.0f;
[HideInInspector]
public float waitTillFire = 0;
public GameObject bullet;
public GameObject bulletSpawn;
public float shootTimer = 0.0f;
public float shootCooler = 0.1f;
public float keyCooler = 0.5f;
public float keyTimer = 0.0f;
// Use this for initialization
void Start ()
{
bulletsLeft = bulletsPerClip;
// hitParticles.emit = false;
}
// Update is called once per frame
void Update ()
{
if (keyTimer > 0)
{
keyTimer -= Time.deltaTime;
}
if (keyTimer < 0)
{
keyTimer = 0;
}
if (shootTimer > 0)
{
shootTimer -= Time.deltaTime;
}
if (shootTimer < 0)
{
shootTimer = 0;
}
if (keyTimer == 0)
{
if (Input.GetMouseButton (0) && bulletsLeft > 0)
{
if (shootTimer == 0)
{
PlayShootAudio();
RayFire();
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
shootTimer = shootCooler;
keyTimer = keyCooler;
}
}
else if (Input.GetMouseButtonDown (0) && clip == 0)
{
PlayEmptyAudio();
shootTimer = shootCooler;
keyTimer = keyCooler;
}
if (Input.GetKey(KeyCode.R))
{
Reload();
shootTimer = shootCooler;
keyTimer = keyCooler;
}
}
}
void RayFire()
{
GameObject.Find("CQAssaultRifle").animation.Play("GunFiring"); // First name is for name of object that will play your animation and second name is what you animation is called
RaycastHit hit;
Vector3 directionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, directionRay * range, Color.yellow);
if (Physics.Raycast(transform.position, directionRay, out hit, range))
{
if (hit.rigidbody)
{
// if (hitParticles)
// {
// hitParticles.transform.position = hit.point;
// hitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
// hitParticles.Emit();
hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
// hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
// }
}
}
bulletsLeft--;
if (bulletsLeft < 0)
{
bulletsLeft = 0;
}
if (bulletsLeft == 0)
{
Reload();
}
}
void Reload()
{
if (clip > 0)
{
StartCoroutine(ReloadSpeed());
clip--;
}
}
IEnumerator ReloadSpeed()
{
if (clip > 0)
{
// GameObject.Find("").animation.Play(""); // First name is for name of object that will play your animation and second name is what you animation is called
PlayReloadAudio();
yield return new WaitForSeconds(reloadTime);
//if (clip > 0)
//{
bulletsLeft = bulletsPerClip;
//}
}
else if (clip < 0)
{
PlayEmptyAudio();
}
}
Your answer
Follow this Question
Related Questions
having trouble with ammo reload script. destorys extra ammo 1 Answer
Gun script not working 3 Answers
FPS UI ammo display problem 0 Answers
how to access a specific instance of a script 1 Answer
Multiple Cars not working 1 Answer