- Home /
How do I make ammo
I have a ammo reload script but I need something to display it and pick up ammo for it
I have no clue what to do and I don't know how to display ammo
here is the script using UnityEngine; using System.Collections; using UnityEngine.Audio; public class GunFire : MonoBehaviour { public float damage = 10f; public float range = 100f; public float fireRate = 15f; public float impactForce = 30f; public int maxAmmo = 120; private int currentAmmo; public float reloadTime = 1f; private bool isReloading = false; public Camera fpsCam; public ParticleSystem muzzleFlash; public GameObject impactEffect; public AudioSource Sound; public AudioSource reloadSound; private float nextTimeToFire = 0f; public Animator anim; void Start () { currentAmmo = maxAmmo; } void OnEnable () { isReloading = false; anim.SetBool ("reload", false); } // Update is called once per frame void Update () { if (isReloading) return; if (currentAmmo <= 0) { StartCoroutine(Reload()); return; } if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire) { nextTimeToFire = Time.time + 1f / fireRate; Shoot(); Sound.Play (); } } IEnumerator Reload () { isReloading = true; Debug.Log ("Reloading..."); reloadSound.Play(); anim.SetBool ("reload", true); yield return new WaitForSeconds (reloadTime); anim.SetBool ("reload", false); currentAmmo = maxAmmo; isReloading = false; } void Shoot () { if (Input.GetButtonDown("Fire1")) Sound.Play(); muzzleFlash.Play(); currentAmmo--; RaycastHit hit; if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) { Debug.Log(hit.transform.name); Enemy enemy = hit.transform.GetComponent<Enemy>(); if (enemy != null) { enemy.TakeDamage(damage); if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * impactForce); } GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal)); Destroy(impactGO, 2f); } } } }
Your answer
Follow this Question
Related Questions
how do i attach a gun to my character 2 Answers
GUI does not show my current ammo 0 Answers
Gun with ammo help 1 Answer
problem with ammo count 1 Answer
Ammo Pick problem im using c# 0 Answers