Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var BulletSpeed = 100; //force of bullets being shot
- var projectile : Rigidbody; //bullet prefab goes here
- var bullets = 30; //amount of bullets per "clip"
- var totalBullets = 60; //total amount of bullets we have available
- var reloadTime = 3; //time to reload
- var reloadAmount = 10;
- var ammo : GUIText; //GUI text for our bullet/ammo count
- var rate : float = 0.5;
- private var rate_time : float;
- var Gun : GameObject;
- var ammoclip : GameObject;
- var flash : GameObject;
- function Update () {
- Fire ();
- Reload ();
- }
- function Fire () {
- if(bullets > 0) { //if we have at least 1 bullet then we can fire
- if (Input.GetButtonDown("Fire1") && Time.time > rate_time) {
- rate_time = Time.time + rate;
- var clone : Rigidbody;// Instantiate the projectile at the position and rotation of this transform
- clone = Instantiate(projectile, transform.position, transform.rotation);
- clone.velocity = transform.TransformDirection (Vector3.forward * BulletSpeed);// Give the cloned object an initial velocity along the current
- AudioSource.PlayClipAtPoint(sound, transform.position, 1); // object's Z axis
- bullets -=1; //subtracts one bullet per fire sequence
- Gun.animation.Play();
- flash.animation.Play();
- }
- }
- }
- function Reload () {
- if(bullets < 1) { //if we have less than 1 bullet then we can reload
- if (totalBullets > 0)
- if(Input.GetKeyDown("r")) {
- AudioSource.PlayClipAtPoint(reloadSound, transform.position, 1); //plays reload soundclip
- yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding bullets
- bullets += reloadAmount; //adds 3 bullets to our "clip"
- totalBullets -= reloadAmount; //subtracts 3 bullets from our totalBullets amount
- ammoclip.animation.Play();
- }
- }
- }
- function OnGUI () {
- ammo.text = "bullets: " + bullets + "/" + totalBullets.ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment