Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 BulletProj : GameObject; // Bullet will fire out of the barrel
- var BulletProjSpawn: GameObject; // spawn point form projectile
- var shootForce : int = -1500;
- function Start()
- {
- animation["fire"].speed = 3; //set the firing animation to a little faster
- animation.wrapMode = WrapMode.Loop;
- animation.Play ("idle", PlayMode.StopAll);//start by playing the idle animation in loop
- }
- function Update()
- {
- if (Input.GetButton ("Fire1") && 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
- }
- else if (Input.GetButton ("Fire1") && animation.IsPlaying("idle") && BulletCount <= 0) //if the bullets left in the clip is 0
- {
- animation.wrapMode = WrapMode.Clamp;
- animation.Play ("reload"); //reload the gun
- }
- if (Input.GetKey(KeyCode.R) && animation.IsPlaying("idle") && BulletCount <30) //if ammunition is less than 30 you can reload
- {
- animation.wrapMode = WrapMode.Clamp;
- animation.Play ("reload"); //reload the gun
- }
- }
- function Reload()
- {
- 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
- var BS = Instantiate(BulletProj, BulletProjSpawn.transform.position, BulletProjSpawn.transform.rotation);
- BS.rigidbody.AddForce(transform.forward * shootForce);
- }
- function BackToIdle() //called via animation event at the end of fire/rotate in order to get back to idle
- {
- animation.wrapMode = WrapMode.Loop;
- animation.Play("idle");
- }
- function EjectBullet() //Called via animation event to eject a bullet
- {
- Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation);
- BulletCount = (BulletCount - 1); //when the bullet is ejected the bullet count reduces by one
- }
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement