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:
-
Attach this script to your camera.
-
Crate a layer, and name it ‘Bullet Holes’ (Or something).
-
Put everything you want bullet holes to appear on in this layer.
-
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.
-
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.