Free WIP shooting script (Supports auto, semi and burst)

Here:

#pragma strict

enum fireMode { semi, auto, burst }

private var mode : fireMode;
private var currentMode : fireMode;
var firstMode : fireMode = fireMode.auto;
var secondMode : fireMode = fireMode.semi;

//Amount of shots in burst fire
var burstAmount : int = 3;
var timeBetweenBursts : float = 0.1;

var range : float = 300; //Range of the weapon

var bulletHole : GameObject; //Bullethole object
var layerMask : LayerMask; //Culling for the bulletholes. Put your terrain objects in a separate layer, and define that here

var shotsPerSecond : float = 0.1; //Shots per second

//Ammunition
//Bullets
var bulletsPerClip : int;
private var bulletsLeft : int;
//Magazines
var magazines : int;
private var magsLeft : int;

//For turning off and on the muzzleflash renderer
var muzzleFlash : GameObject;

private var canFire : boolean;
private var shotTimer : float;

function Start()
{
bulletsLeft = bulletsPerClip;
magsLeft = magazines;
mode = firstMode;
}

function Update()
{
	muzzleFlash.renderer.enabled = false;
	
	if(Input.GetMouseButtonDown(0)  currentMode == fireMode.semi  canFire  bulletsLeft > 0)
	{
		fireOneShot();
	}
	
	if(Input.GetMouseButtonDown(0)  currentMode == fireMode.burst  canFire  bulletsLeft > 0)
	{
		fireBurst();
	}
	
	if(Input.GetKey(KeyCode.Mouse0)  currentMode == fireMode.auto  canFire  bulletsLeft > 0)
	{
		fireOneShot();
	}
	
	if(shotTimer == 0)
	{
		canFire = true;
	} else {
		canFire = false;
	}
	
	shotTimer -= Time.deltaTime;
	
	if(shotTimer < 0)
	{
	shotTimer = 0;
	}
	
	if(Input.GetKeyDown(KeyCode.R))
	{
	Reload();
	}
	
	currentMode = mode;
	
	if(Input.GetKeyDown(KeyCode.Q)  mode == firstMode)
	{
	mode = secondMode;
	} else if(Input.GetKeyDown(KeyCode.Q)  mode == secondMode)
	{
	mode = firstMode;
	}
}

function fireOneShot()
{
	var hit : RaycastHit;
	//var dir = transform.TransformDirection (Vector3.forward);
	var dir = gameObject.transform.TransformDirection(Vector3(Random.Range(-0.05, 0.05), Random.Range(-0.05, 0.05),1));
	    
	if(Physics.Raycast(transform.position, dir, hit, range, layerMask))
	{
		var contact = hit.point;
		var rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
		            
		var bulletHole = Instantiate (bulletHole, contact, rotation) as GameObject;
		bulletHole.transform.position.y = bulletHole.transform.position.y + 0.1;
		//bulletHole.transform.position = Vector3(0,0.1,0.1);
		
		shotTimer = 0.1;
		bulletsLeft--;
		muzzleFlash.renderer.enabled = true;
	}
}

function fireBurst()
{
		for(var i = 0; i < burstAmount; i++)
		{
		fireOneShot();
		yield WaitForSeconds(timeBetweenBursts);
		}
}


function Reload()
{
	if(magsLeft > 0)
	{
	yield WaitForSeconds(3);
	bulletsLeft = bulletsPerClip;
	magsLeft--;
	}
}

function OnGUI()
{
    GUI.Label (Rect (10, 10, 100, 20), "Bullets Left:" + bulletsLeft);
    GUI.Label (Rect (10, 30, 100, 20), "Mags Left:" + magsLeft);

}

If anyone wants me to, I can comment more of this to explain it. Please note, this script is still a WIP. I am just getting the basics down before I add in animations and sound, etc.

Currently supports:

  • Automatic shooting (Shots per second conifgurable)
  • Semiautomatic shooting
  • Burst fire (Burst amount, and time between shots configurable)
  • Range (Configurable)
  • Bullets and magazines
  • Bullet holes (Still in beta, works well on flat ground)
  • Layer masking said bulletHoles (You have to for the collisions to work)
  • Muzzle flash

How to set up:

  1. Attach this script to your camera.

  2. Crate a layer, and name it ‘Bullet Holes’ (Or something).

  3. Put everything you want bullet holes to appear on in this layer.

  4. Create a plane in front of your gun, and apply the muzzle flash texture to it. It should be visible. Put that plane into the ‘Muzzle Flash’ variable of the script.

  5. Create another plane, and add your bullet hole texture to it. Again, apply this to the ‘Bullet Hole’ variable in the script.

If you want anything more in this script, don’t hesitate to ask! I am trying to create the FPS script to end all FPS scripts

Left mouse click to shoot. Q to change fire modes.

#pragma strict

enum fireMode { semi, auto, burst }

private var mode : fireMode;
private var currentMode : fireMode;
var firstMode : fireMode = fireMode.auto;
var secondMode : fireMode = fireMode.semi;

//Damage
var damage : int = 50;

//Amount of shots in burst fire
var burstAmount : int = 3;
var timeBetweenBursts : float = 0.05;

var range : float = 300; //Range of the weapon

var bulletHole : GameObject; //Bullethole object
var layerMask : LayerMask; //Culling for the bulletholes. Put your terrain objects in a separate layer, and define that here

var shotsPerSecond : float = 0.1; //Shots per second

//Ammunition
//Bullets
var bulletsPerClip : int = 50;
private var bulletsLeft : int;
//Magazines
var magazines : int = 50;
private var magsLeft : int;

//For turning off and on the muzzleflash renderer
var muzzleFlash : GameObject;

private var canFire : boolean;
private var shotTimer : float;

function Start()
{
bulletsLeft = bulletsPerClip;
magsLeft = magazines;
mode = firstMode;
}

function Update()
{
	muzzleFlash.renderer.enabled = false;
	
	if(Input.GetMouseButtonDown(0)  currentMode == fireMode.semi  canFire  bulletsLeft > 0)
	{
		fireOneShot();
	}
	
	if(Input.GetMouseButtonDown(0)  currentMode == fireMode.burst  canFire  bulletsLeft > 0)
	{
		fireBurst();
	}
	
	if(Input.GetKey(KeyCode.Mouse0)  currentMode == fireMode.auto  canFire  bulletsLeft > 0)
	{
		fireOneShot();
	}
	
	if(shotTimer == 0)
	{
		canFire = true;
	} else {
		canFire = false;
	}
	
	shotTimer -= Time.deltaTime;
	
	if(shotTimer < 0)
	{
	shotTimer = 0;
	}
	
	if(Input.GetKeyDown(KeyCode.R))
	{
	Reload();
	}
	
	currentMode = mode;
	
	if(Input.GetKeyDown(KeyCode.Z)  mode == firstMode)
	{
	mode = secondMode;
	} else if(Input.GetKeyDown(KeyCode.Z)  mode == secondMode)
	{
	mode = firstMode;
	}
}

function fireOneShot()
{
	var hit : RaycastHit;
	//var dir = transform.TransformDirection (Vector3.forward);
	var dir = gameObject.transform.TransformDirection(Vector3(Random.Range(-0.05, 0.05), Random.Range(-0.05, 0.05),1));
	    
	if(Physics.Raycast(transform.position, dir, hit, range, layerMask))
	{
		var contact = hit.point;
		var rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
				
		var bulletHole = Instantiate(bulletHole,hit.point + (hit.normal*0.01),Quaternion.LookRotation(hit.normal));
		bulletHole.transform.parent = hit.transform;
		//clone1.transform.parent = transform;
		Destroy(bulletHole, 5);
		
		shotTimer = shotsPerSecond;
		bulletsLeft--;
		muzzleFlash.renderer.enabled = true;
    	var f : float = Random.Range(-500,500);
    	SendDamage(hit.transform.gameObject);
	}
}

function fireBurst()
{
		for(var i = 0; i < burstAmount; i++)
		{
		fireOneShot();
		yield WaitForSeconds(timeBetweenBursts);
		}
}


function Reload()
{
	if(magsLeft > 0)
	{
	yield WaitForSeconds(3);
	bulletsLeft = bulletsPerClip;
	magsLeft--;
	}
}

function OnGUI()
{
    GUI.Label (Rect (10, 10, 100, 20), "Bullets Left:" + bulletsLeft);
    GUI.Label (Rect (10, 30, 100, 20), "Mags Left:" + magsLeft);

}

function SendDamage(x : GameObject)
{
x.collider.SendMessageUpwards ("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}

Fixes bullet holes - they now work perfectly! Now, they display properly, and follow the rotation of the object they are on, so if you shoot a rigidbody the bullet holes will follow the rigidbody!

Also supports damage! Add this script to the thing you want to take damage:

#pragma strict

var health : int = 100;

function Update()
{
	if(health <= 0)
	{
	//Do stuff
	Destroy(this.gameObject);
	}
}

function ApplyDamage(damage : int)
{
	health = health - damage;
}

Also, the Shots Per Second variable was buggy, and I fixed it. Works well now. Also, for usage with the Ultimate FPS Camera, to change fire modes (auto, semi, burst) press z.