var BulletCount : int; //Ammount of bullets remaining in the clip
var MagCapacity = 10; //The maximum capacity of bullets in the clip
var MuzzleFlash : GameObject; //The muzzleFlash prefab
var MuzzleFlashSpawn : GameObject; //The muzzleFlash spawn point object
var BulletSpawn : GameObject; //The ejected bullet spawn point object
var Bullet : GameObject; //The bullet prefab that gets ejected
var bulletSound : AudioClip;
var compensationTime = .1;
function Start()
{
animation["fire"].speed = 3;
animation["fire"].wrapMode = WrapMode.Clamp;
animation["idle"].wrapMode = WrapMode.Loop;
animation.Play ("idle", PlayMode.StopAll); //start by playing the idle animation in loop
}
function Update ()
{
if (Input.GetKey ("E") animation.IsPlaying("idle") BulletCount >= 1) //check to see if ready to fire
{
animation.wrapMode = WrapMode.Clamp; //using clamp allows the detection of the end of the animation to be more accurate
animation.Play ("fire"); //play the firing animation
SendMessage("FireBullet");
SendMessage("SoundEffect", bulletSound);
}
else if (Input.GetKey ("E") animation.IsPlaying("idle") BulletCount <= 0) //if the bullets left in the clip is 0
{
SendMessage("Reload");
}
else {
animation.Play("idle");
}
}
function Reload ()
{
yield WaitForSeconds(compensationTime);
BulletCount = MagCapacity; //set the bullet count to = the maximum clip size
}
function FireBullet () //Called via animation event to make the muzzle flash
{
MuzzleFlashSpawn.transform.Rotate(Vector3.up * Random.Range(0,360)); //set the spawn point to have a random rotation vector
Instantiate(MuzzleFlash, MuzzleFlashSpawn.transform.position, MuzzleFlashSpawn.transform.rotation); //instantiate the muzzle flash
}
function SoundEffect (clip : AudioClip) //Called via animation event to make a sound effect
{
var randomPitch = (Random.Range(1.0,2.0));
audio.pitch = randomPitch;
audio.PlayOneShot(clip);
}
JM, your animation script is pretty solid, except for the way your changing the WrapMode in Update(); you’re changing the WrapMode of ALL of this object’s animations every frame, just because you want to play one animation with that WrapMode, which is a massive waste of CPU cycles.
You could set the WrapMode of specific animations in Start(), like this:
animation["animName"].wrapMode = WrapMode.Clamp;
That way you don’t have to keep flip-flopping WrapModes every frame in Update()
It looks like you set the WrapMode of those individual animations to “loop” in Start(), but you’re still changing the WrapMode of all animations to clamp right before you play the animations you want Clamped in Update(), even though those are the only animations you want to clamp.
I think it would be something more like this:
function Start()
{
animation["fire"].speed = 1;
animation.wrapMode = WrapMode.Loop; //set all animations to loop
//except fire and reload, set them to clamp
animation["fire"].wrapMode = WrapMode.Clamp;
animation["reload"].wrapMode = WrapMode.Clamp;
animation.Play ("idle", PlayMode.StopAll); //start by playing the idle animation in loop
}
function Update ()
{
if (Input.GetKey ("Fe") animation.IsPlaying("idle") BulletCount >= 1) //check to see if ready to fire
{
animation.Play ("fire"); //play the firing animation
}
else if (Input.GetKey ("E") animation.IsPlaying("idle") BulletCount <= 0) //if the bullets left in the clip is 0
{
animation.Play ("reload"); //reload the gun
}
}