Hello i am trying to make a small fps game in unity but i’am having some trouble with trying to get the gun ammo to update here is the code i’am using. Is there anything i have done wrong because the ammo is not updating and the line i want it to draw when shooting is also not working does anyone know what is wrong?
var Range : float = 1000;
var Force : float = 1000;
var Clips : int = 2;
var BulletsPerClip : int = 36;
var ReloadTimer : float = 0;
var ReloadCooler : float = 3.3;
var Damage : int = 10;
var BulletsLeft : int = 36;
var ShotTimer : float = 0;
var ShotCooler : float = 0.09;
public var ShotSound : AudioClip;
public var ReloadSound : AudioClip;
public var EmptySound : AudioClip;
var HitParticles : ParticleEmitter;
function Start ()
{
BulletsLeft = BulletsPerClip;
HitParticles.emit = false;
}
function Update ()
{
if(ReloadTimer > 0)
{
ReloadTimer -= Time.deltaTime;
}
if(ReloadTimer < 0)
{
ReloadTimer = 0;
}
if(ShotTimer > 0)
{
ShotTimer -= Time.deltaTime;
}
if(ShotTimer < 0)
{
ShotTimer = 0;
}
if (BulletsLeft < 0)
{
BulletsLeft = 0;
}
if(Input.GetMouseButtonDown(0) ShotTimer == 0)
{
if(BulletsLeft > 0)
{
if( ShotTimer == 0)
{
PlayShotSound();
ShotTimer = ShotCooler;
Shot();
BulletsLeft --;
}
}
if(BulletsLeft == 0 Clips == 0)
{
if( ShotTimer == 0)
{
PlayEmptySound();
ShotTimer = ShotCooler;
}
}
if(BulletsLeft == 0 Clips > 0 ReloadTimer == 0)
{
PlayReloadSound();
Reload();
}
}
}
function Shot ()
{
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.posision , DirectionRay * Range , Color.red);
if(Physics.Raycast(transform.position , DirectionRay , Hit, Range ))
{
if(Hit.rigidbody)
{
if(HitParticles)
{
HitParticles.transform.position = Hit.point;
HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
HitParticles.Emit ();
Hit.rigidbody.AddForceAtPosition( DirectionRay * Force , Hit.point);
Hit.collider.SendMessageUpwards ("ApplyDamage" , Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
function Reload ()
{
ReloadTimer = ReloadCooler;
yield WaitForSeconds(ReloadTimer);
BulletsLeft = BulletsPerClip;
Clips --;
}
function PlayShotSound ()
{
audio.PlayOneShot (ShotSound);
}
function PlayReloadSound ()
{
audio.PlayOneShot (ReloadSound);
}
function PlayEmptySound ()
{
audio.PlayOneShot (EmptySound);
}