What is a good script for a gun to fire and make recoil and damage?

Hello, I am now on a zombie game project and I need a little help. Well, actually a lot of help about scripting to make my gun fire. I am now on an assault rifle that I want to be automatic that has a medium fire rate (I could add that in by myself). I just want a throe
script. I am sorry for this question but I am hungry for a good, juicy script. Please help me?

Damage can be done by sending the object a message. A basic example is:

gameObject.SendMessage("applyDamage", damage, SendMessageOptions.DontRequireReceiver);

This tells the gameObject to run the method “applyDamage()” where damage is the given parameter and DontRequireReceiver makes it so it won’t bomb if the object doesn’t have an applyDamage() method.

For recoil you could just tie in the character camera with the gun. Each shot that’s fired angles the camera up a little bit, perhaps to a maximum angle (say 35 degrees or something). I’ve never done recoil myself, but I have done random spread with my cast rays.

If you’re still interested in a drag and drop, here is a script I started working on a while back (it is incomplete so you aren’t allowed to complain if parts are stubs or just don’t work at all). It can even be used for melee weapons by making the range really short.
(If someone can tell me how to make code scrollable that would be wonderful…)

 * Created by: ArmoredThirteen
 * 9/2/2012
 * This script is open source. Credit is nice but not mandatory.

using UnityEngine;
using System.Collections;

 * Universal gun class used for firing weapons. With some
 * tweaking it may work for melee weapons as well.


public class Gun_Universal : MonoBehaviour {
	 * displayName: The name given for display purposes.
	 * rayTrace: If true don't use physical projectiles
	 * 		and instead raytrace the bullets.
	 * minDamage: Minimum damage it can do.
	 * maxDamage: Maximum damage it can do.
	 * bullet: The GameObject used for physical bullets.
	 * bulletHole: Used for raytraced bullets, this is the
	 * 		bullet hole left where it hits.
	 * hitEffect: Used for raytraced bullets, this is an
	 * 		effect that gets played where it hits.
	 * blastEffect: Used for an explosive effect as apposed
	 * 		to the typical min/max damage.
	 * accuracy: How far off the orthogonal vector3 can be.
	 * 		A higher number is less accurate while 0 is a
	 * 		perfect accuracy.
	 * range: The maximum distance the raytrace bullet is
	 * 		still effective at.
	 * fireRate: How often a bullet can be fired
	 * warmupTime: Time it takes for first shot to fire
	 * holdFire: If true you can hold the fire button. If
	 * 		false you must click/press for every shot.
	 * ammoPerClip: Maximum ammo per clip. Set to zero if
	 * 		you want it to be infinite.
	 * curClipAmmo: Current clip's ammo count.
	 * maxAmmo: most ammo in pouch weapon can have. Set to
	 * 		zero if you want it to be infinite.
	 * curAmmo: Current amount of ammo in pouch.
	 * automaticReload: If true, weapon reloads when the
	 * 		clip is empty automatically. Triggers when clip
	 * 		is empty and weapon attempts to fire.
	 * reloadTime: Amount of time it takes to reload a clip.
	 * sound-fire: Plays every time a bullet fires.
	 * sound-constant: Plays as long as the weapon is out.
	 * sound-active: Plays only when weapon is warmed up
	 * 		or actively firing.
	 * sound-warmup: Plays if a weapon is warming up.
	 * sound-cooldown: Plays if weapon is cooling down.
	 * sound-reload: Plays every time a clip reloads.
	 * sound-empty: Plays when completely out of ammo.
	 * sound-birth: Plays when object comes into existance.
	 * timer_fireRate: Times when it is able to fire.
	 * timer-reloadTime: Times the reloading sequence.
	public string displayName = "Happy Gun";
	public bool rayTrace = true;
	public int minDamage = 10;
	public int maxDamage = 15;
	public GameObject bullet;
	public GameObject bulletHole;
	public GameObject hitEffect;
	public GameObject blastEffect;
	public float accuracy = 0.5f;
	public float range = 250.0f;
	public float fireRate = 0.2f;
	public float warmupTime = 0.0f;
	public bool holdFire = true;
	public int ammoPerClip = 12;
	public int curClipAmmo;
	public int maxAmmo = 80;
	public int curAmmo;
	public bool automaticReload = true;
	public float reloadTime = 2.0f;
	public AudioClip sound_fire;
	public AudioClip sound_constant;
	public AudioClip sound_active;
	public AudioClip sound_warmup;
	public AudioClip sound_cooldown;
	public AudioClip sound_reload;
	public AudioClip sound_empty;
	public AudioClip sound_birth;
	private float timer_fireRate;
	private float timer_reloadTime;
	public bool toggle_isReloading = false;
	public void Start() {
		// if can play sound
		if(sound_birth != null) {
			// play it once
			audio.PlayOneShot(sound_birth, 3.0F);
		curClipAmmo = ammoPerClip;
		curAmmo = maxAmmo / 2;
		timer_fireRate = Time.time;
		timer_reloadTime = Time.time;
	public void Update() {
		// if can play sound and isn't already
		if(!audio.isPlaying) {
	public void fireShot() {
		timer_fireRate -= Time.deltaTime;
		// current clip has ammo and can fire
		if(curClipAmmo > 0 && Time.time >= timer_fireRate) {
			if(rayTrace) {
			else {
			timer_fireRate = Time.time + fireRate;
			// if can play sound
			if(sound_fire != null) {
				// play it once
				audio.PlayOneShot(sound_fire, 1.0F);
		// empty clip but has ammo
		else if(curClipAmmo <= 0 && curAmmo > 0 && automaticReload) {
			if(!toggle_isReloading) {
				toggle_isReloading = true;
				timer_reloadTime = Time.time + reloadTime;
				// if can play sound
				if(sound_reload != null) {
					// play it once
			else {
		// empty everything
		else if(curClipAmmo <= 0 && curAmmo <= 0) {
			// if can play sound
			if(sound_empty != null) {
				// play it once
	// fire the weapon using raytrace rules
	public void fireRaytrace() {
		RaycastHit theHit = new RaycastHit();
		Vector3 fireDirection = gameObject.transform.forward;
		Vector3 position = gameObject.transform.position;
		if(accuracy > 0.0f) {
			fireDirection = randomAccuracy(fireDirection);
		// if it hit something
		//Debug.DrawLine(position, (fireDirection.normalized*range)+position, Color.red, 0.25f);
		if(Physics.Raycast(position, fireDirection, out theHit, range)) {
			Debug.DrawLine(position, theHit.point, Color.red, 0.25f);
			// has no blast radius
			if(blastEffect == null) {
				// roll damage and apply
				int damage = Random.Range(minDamage, maxDamage+1);
				theHit.collider.gameObject.SendMessage("applyDamage", damage, SendMessageOptions.DontRequireReceiver);
			// has a blast radius
			else {
				Instantiate(blastEffect, theHit.rigidbody.position, theHit.transform.rotation);
			// has a bullet hole
			if(bulletHole != null) {
			// has a hit effect
			if(hitEffect != null) {
				Instantiate(hitEffect, theHit.rigidbody.position, theHit.transform.rotation);
	// fire the weapon using physical bullets
	public void firePhysical() {
	// reloading method for button presses
	public void forceReload() {
		if(!toggle_isReloading) {
			toggle_isReloading = true;
			timer_reloadTime = Time.time + reloadTime;
	// everything you need for reloading
	private void reloading() {
		// proper amount of time has passed
		if(Time.time >= timer_reloadTime) {
			toggle_isReloading = false;
			int clipAmount = 0;
			// can reload full clip
			if(curAmmo >= ammoPerClip - curClipAmmo) {
				clipAmount = ammoPerClip - curClipAmmo;
			// can only reload partial clip
			else {
				clipAmount = curAmmo;
			curAmmo -= clipAmount;
			curClipAmmo += clipAmount;
	// adds ammo to the gun
	public void addAmmo(int rounds) {
		curAmmo += rounds;
		if(curAmmo > maxAmmo) {
			curAmmo = maxAmmo;
		// just in case you want to subtract ammo
		else if(curAmmo < 0) {
			curAmmo = 0;
	// similar to add ammo, but does in in clip sizes
	public void addClips(int clips) {
		addAmmo(clips * ammoPerClip);
	// adds a bullet hole to the hit object
	private void addBulletHole(RaycastHit theHit) {
		// create the game object at the hit location
		GameObject temp = (GameObject)Instantiate(bulletHole, theHit.point, Quaternion.identity);
		// align it to the hit normal and then randomly rotate
		temp.transform.forward = theHit.normal;
		temp.transform.Rotate(0, 0, Random.Range(0, 360));
		// finally, parent it to the hit object
		temp.transform.parent = theHit.transform;
	// weights hit direction with a random direction
	private Vector3 randomAccuracy(Vector3 forward) {
		// get random vector than make it orthogonal to forward
		Vector3 fireDirection = Random.onUnitSphere;
		Vector3.OrthoNormalize(ref forward, ref fireDirection);
		// set the magnitude
		forward *= 10.0f;
		fireDirection *= Random.Range(0.0f, accuracy/5);
		// return resulting vector3
		return fireDirection + forward;