hello again, this is a question that most might have seen more than once, but i have been trying to find the best way to top up ammo with this script.
//shootScript.js
var pelletPrefab : Rigidbody;
var pelletSpeed : float = 150;
var pelletCount : int = 5;
var spreadFactor : float = 0.01;
var fireRate : float = 0.5;
var target : Transform;
var MaxAmmo = 6;
var CurrentAmmo = 6;
var canFire = true;
var canShootAgain = true;
var shotsFired = 0;
var CurrentAmmoGUI : GUIText;
private var nextFire : float = 0.0;
function Awake ()
{
if (!target)
{
target = GameObject.FindWithTag ("Player").transform;
}
}
function Update()
{
OnGUI();
Shooting();
}
function Shooting() {
if (canFire == true)
{
if(canShootAgain == true)
if(Input.GetButtonDown("Fire1") && Time.time > nextFire){
canShootAgain = false;
var pellet : Rigidbody;
for(var i = 0; i<pelletCount; i++){
var pelletRot = transform.rotation;
pelletRot.x += Random.Range(-spreadFactor, spreadFactor);
pelletRot.y += Random.Range(-spreadFactor, spreadFactor);
pellet = Instantiate(pelletPrefab, transform.position, pelletRot);
pellet.velocity = transform.forward*pelletSpeed;
CurrentAmmo -= 1;
shotsFired +=1;
canShootAgain = true;
if(CurrentAmmo == 0)
{
canFire = false;
if(canFire == false){
yield WaitForSeconds(0.2);
audio.Play();
}
}
nextFire = Time.time + fireRate;
}
}
}
}
function OnGUI(){
CurrentAmmoGUI.text = "" +CurrentAmmo.ToString(); //displays our current health status
}
i’v tried a good deal of ways, but it just doesn’t seem to work, when the gun runs out of ammo, i go to the ammo box, and try to Top up the ammo count, and nothing happens.
if anyone could point out what i’m doing wrong here, that would be helpful.
thank you
- Ownerfate
extra details:
the ammo box does not have a tag , nor did the scripts refer to the item in any sense.
(i have even tried with tags and names, still didn’t work)