Hey, I am new to scripting in Unity and decided to sit down and create some psuedocode for a reload script in C#. I started by listing variables that I believe would play an important role in a reloading script. Everything works just as I planned (to my surprise) however my issue is that when I click 'R' to reload, it subtracts a different number from my variable 'AmmoToReloadFrom' (spare ammo). My first time posting as well so don't be too harsh about any mistakes :) Thank you.
public class ShootGun : MonoBehaviour {
public Rigidbody bullet;
public Transform bulletSpawn;
public float bulletSpeed;
public float AmmoToReloadFrom;
public float MagSize;
public float CurrentAmmo;
public bool CanShoot = false;
void Start () {
}
void Update () {
if (Input.GetButton("Fire1") && CurrentAmmo > 0) {
CanShoot = true;
var bulletPrefab = (Rigidbody)Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
bulletPrefab.rigidbody.AddForce(-transform.right * bulletSpeed);
audio.Play();
CurrentAmmo--;
}
if (Input.GetButton("Reload") && AmmoToReloadFrom > 0) {
CurrentAmmo = MagSize;
AmmoToReloadFrom = AmmoToReloadFrom - 20;
}
}
void OnGUI() {
if (CurrentAmmo <= 0 && AmmoToReloadFrom > 0) {
GUI.Label(new Rect (Screen.width / 2 - 100, Screen.height / 2 - 100, 250, 200), "Press R To Reload");
}
if (AmmoToReloadFrom <= 0 && CurrentAmmo <= 0) {
GUI.Label(new Rect (Screen.width / 2 - 100,Screen.height / 2 - 100, 250, 200), "No Ammo");
}
}
}
↧