Hello i’m making a fps and i’m noob at script so can you help me please?
i got a plane with a 3D Text on it ( Ammo Text ), it look like a HUD, and i want when i shoot with mouse1 the number of Text Ammo decrease.
Thanks !
In order for this to work you will have to make some things for the gun to shoot such as a bullet prefab, spawn point, shot sound, and bullet speed. if you already have a gun script then just use the ammo parts of this.
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
public Rigidbody bulletPrefab;
public Transform spawnPoint;
public int speed;
public float nextFire = 0.0f;
public float fireRate = 0.1f;
public int ammo = 150;
public GUIText ammoText;
public AudioSource shotSound;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.Mouse0) && Time.time > nextFire)
{
if(ammo > 0)
{
Shot();
audio.Play();
ammo --;
}
}
}
void Shot()
{
nextFire = Time.time + fireRate;
Rigidbody bulletInstance;
bulletInstance = Instantiate(bulletPrefab, spawnPoint.position, bulletPrefab.transform.rotation) as Rigidbody;
bulletInstance.AddForce(spawnPoint.forward * speed);
}
void OnGUI()
{
ammoText.text = "Ammo: " + ammo.ToString();
}
}