I am trying to have my player reload after they shoot 5 bullets...here is the script im working with..but the problem is, when i set "shotsFired = 0", my animation doesn't play...but when i add shotsfired = 0 inside the Reload function, the animation plays but doesn't reload after 5 shots...
private var walkSpeed : float = 10.0;
private var gravity = 100.0;
private var moveDirection : Vector3 = Vector3.zero;
private var charController : CharacterController;
private var idle = true;
private var aiming = false;
public var bulletPrefab : Transform;
var muzzle : Transform;
private var nextFire = 0.0;
var fireRate = .5;
static var hasBasementKey = false;
static var hasfirstfloorkey = false;
static var hassecondfloorkey = false;
var gunShot : AudioClip;
//var Breathing : AudioClip;
var nextPlay = 0.0;
var playRate = 5;
private var ammo = 50;
var reloading = false;
var reloadTime = 3;
var bullets = 10;
var shotsFired = 0;
function Start()
{
charController = GetComponent(CharacterController);
animation.wrapMode = WrapMode.Loop;
animation["gun"].wrapMode = WrapMode.ClampForever;
animation["reload"].wrapMode = WrapMode.ClampForever;
animation["reload"].speed = 1;
//animation["gun"].layer = 1;
}
function Update ()
{
// GameObject.Find("g_count").guiText.text=""+ammo;
Debug.Log(shotsFired);
if(charController.isGrounded == true)
{
if(Input.GetAxis("Vertical") > .1 && reloading == false)
{
if(Input.GetButton("Run"))
{
//audio.clip = Breathing;
//playSound();
animation.CrossFade("run");
walkSpeed = 15;
idle = false;
//Debug.Log(idle);
}
else
{
animation["walk"].speed = 1;
animation.CrossFade("walk");
walkSpeed = 8;
idle = true;
}
}
else if(Input.GetAxis("Vertical") < -.1)
{
animation["walk"].speed = -1;
animation.CrossFade("walk");
walkSpeed = 8;
idle = false;
}
else
{
animation.CrossFade("idle");
idle = true;
}
if(idle == true)
{
if(Input.GetButton("Aim"))
{
aiming = true;
animation.CrossFade("gun");
if(Input.GetButtonDown("Shoot") && Time.time > nextFire && reloading==false)
{
if(ammo > 0)
{
//audio.clip = gunShot;
AudioSource.PlayClipAtPoint(gunShot, transform.position);
nextFire = Time.time + fireRate;
shotsFired++;
//Debug.Log(shotsFired);
ammo--;
shoot();
}
}
}
else
{
aiming = false;
}
}
if(shotsFired == 5)
{
//shotsFired = 0;
Reload();
}
// Create an animation cycle for when the character is turning on the spot
if(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical"))
{
idle = false;
//animation.CrossFade("walk");
}
transform.eulerAngles.y += Input.GetAxis("Horizontal") * 5;
// Calculate the movement direction (forward motion)
moveDirection = Vector3(0,0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
}
if(aiming == false)
{
moveDirection.y -= gravity * Time.deltaTime;
charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
}
}
function shoot()
{
//yield WaitForSeconds(1);
var bulletCreate = Instantiate(bulletPrefab, GameObject.Find("ShootPoint"). transform.position, Quaternion.identity);
var muzzleFlash = Instantiate(muzzle, GameObject.Find("ShootPoint").transform.position, Quaternion.identity);
bulletCreate.rigidbody.AddForce(transform.forward * 6000);
}
function playSound()
{
if(Time.time > nextPlay)
{
nextPlay = Time.time + playRate;
// audio.PlayOneShot(Breathing);
}
}
function Reload()
{
//Debug.Log("reloading");
animation.CrossFade("reload");
reloading = true;
yield WaitForSeconds (reloadTime);
shotsFired=0;
reloading = false;
}
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20),"bullets left: " + ammo);
}