Automatic gun script

I made a script for someone in that past, that incorporated animations and projectiles!! here it is!!

var fireSound : AudioClip;
var clipSize = 10;
var compTime = 1;
private var bullets : int;
var clips = 10;
var barrel : Transform;
var projectile : Rigidbody;
var speed = 30.0;
var reload : boolean = false;

function Awake () {
	animation["fire"].speed = 2;
	animation["idle"].wrapMode = WrapMode.Loop;
	animation["fire"].wrapMode = WrapMode.Clamp;
	animation["reload"].wrapMode = WrapMode.Clamp;
	animation.Play("idle", PlayMode.StopAll);
	
	bullets = clipSize;
}

function Update () {
	if (clips >= 0) {
		if (Input.GetKey("e")  bullets >= 1  animation.IsPlaying("idle")) {
			SendMessage("Fire");
		}
	
		else if (bullets <= 0  !reload) {
			reload = true;
			SendMessage("Reload");
			SendMessage("InvokeReload");
		}
	
		else if (!animation.isPlaying) {
			SendMessage("Idle");
		}
	}
	
	if (bullets > clipSize) {
		bullets = clipSize;
	}
	
	if (clips <= 0) {
		SendMessage("Idle");
	}
}	

function Fire () {
	animation.Play("fire");
	
	bullets -= 1;
	
	var Projectile : Rigidbody = Instantiate(projectile, barrel.position, barrel.rotation);
	Projectile.velocity = transform.TransformDirection(Vector3 (speed, 0, 0));

	if (fireSound)
		AudioSource.PlayClipAtPoint(fireSound, transform.position);	
}

function Reload () {
	animation.Play("reload", PlayMode.StopAll);
	yield WaitForSeconds(animation["reload"].length);
	bullets = clipSize;
	
	reload = false;
}

function Idle () {
	animation.Play("idle");
}

function InvokeReload () {
	clips -= 1;
}

function OnGUI () {
	GUI.Box( Rect(Screen.width - 110,Screen.height - 30, 100, 25), "Clips left " + (clips));
	
	GUI.Box( Rect(Screen.width - 110,Screen.height - 70, 100, 25), "Bullets left " + (bullets));
}

hope some people can use it!! I don’t care if you use it in your game, and if you want, you can put my company name in the credits. NOTE: this script requires a reload animation, fire animation, and idle animation.

Bump

why does like %80 of the untiy community make gun games rofl…

because they never were in the army i guess :slight_smile:

nice script JM :slight_smile:

thanks, but this was a script I made for someone else, not for a game of my own.

bump

guns are fun to blow things up with.

is this like a gun script for a stationary turret?

nice job, i will use it for my Turrets. Hope it works.

this is a script for just a gun, but if you have the animations, i guess you could use it for a turret

and the lord spake saying that this script shall be used in other peoples games

fly into others games, oh mighty script!!

how do u slow the rate u fire??

Welcome to the internet my friend, enjoy your stay.

1 Like

change animation["fire"].speed = 2;to 1 or something

any takers?

Old, but can u recreate script so that it dosent need animations D: D: D:…

I’d change it to a public variable. Inspector edits FTW.

Looks interesting, I am needing a good projectile/missile system.

Just wondering…what happens to your projectile once fired. Seems there is nothing to “kill” it, so it will liver forever in the scene/memory?

well, that would be for a completely different script

It is not the gun’s responsibility to destroy the bullet.

On your projectile, add a script with:

public var lifetime : float;

function Start () {

    Invoke ("Kill", lifetime);

}

function Kill() {
    
    Destroy (this.gameObject);

}