Question by
yusufkaan300 · Apr 05, 2020 at 02:17 PM ·
fpsgame
I was making a ammo/reload system for my fps game but my script doesnt work.WHY?
Here is my script:
public class FireSystem : MonoBehaviour {
public float reloadcooldown;
public float AmmoInGun;
public float AmmoInPocket;
public float AmmoMax;
float AddableAmmo;
float reloadtimer;
public Text AmmoCounter;
public Text PocketAmmoCounter;
public GameObject impactEffect;
RaycastHit hit;
public GameObject RayPoint;
public CharacterController Karakter;
Animator GunAnimset;
public bool CanFire;
float gunTimer;
public float gunCooldown;
public ParticleSystem MuzzleFlash;
AudioSource SesKaynak;
public AudioClip FireSound;
public float range;
public AudioClip ReloadSound;
// Start is called before the first frame update
void Start () {
SesKaynak = GetComponent<AudioSource>();
GunAnimset = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
GunAnimset.SetFloat("hız", Karakter.velocity.magnitude);
AmmoCounter.text = AmmoInGun.ToString();
PocketAmmoCounter.text = AmmoInPocket.ToString();
AddableAmmo = AmmoMax - AmmoInGun;
if (AddableAmmo > AmmoInPocket)
{
AddableAmmo = AmmoInPocket;
}
if (Input.GetKeyDown(KeyCode.R) && AddableAmmo > 0 && AmmoInPocket > 0)
{
if (Time.time > reloadtimer)
{
StartCoroutine(Reload());
reloadtimer = Time.time + reloadcooldown;
}
}
if (Input.GetKey(KeyCode.Mouse0) && CanFire == true && Time.time > gunTimer && AmmoInGun > 0 )
{
Fire();
gunTimer = Time.time + gunCooldown;
}
}
void Fire()
{
AmmoInGun--;
if (Physics.Raycast(RayPoint.transform.position, RayPoint.transform.forward, out hit, range))
{
MuzzleFlash.Play();
SesKaynak.Play();
SesKaynak.clip = FireSound;
Debug.Log(hit.transform.name);
GunAnimset.Play("fire", -1, 0f);
Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
}
}
IEnumerator Reload()
{
GunAnimset.SetBool("isReloading", true);
SesKaynak.clip = ReloadSound;
SesKaynak.Play();
yield return new WaitForSeconds(0.3f);
GunAnimset.SetBool("isReloading", false);
yield return new WaitForSeconds(1.4f);
AmmoInGun = AmmoInGun + AddableAmmo;
AmmoInPocket = AmmoInPocket + AddableAmmo;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612230546im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Game Window Slow and Console Spam 0 Answers
How do i move prefabs into blender??? 5 Answers
Control the sun in game time 0 Answers
How to make speed go slower? 1 Answer