FPS simple shooting gun script

Hi everyone,

I am a complete newbie to coding and have created my own gun script to share it with you guys :smile:. Since i am a novice the script has been included below for people like me who are new to the whole coding aspect of unity so they can study the code and possibly learn from it. I have included line by line detailed comments on the code and it is free to use in your projects as you wish. For the more advanced coders I am looking forward to your feedback and suggestions on what can be improved and how you would do so.

Efficiency of the code may not be great and I am working on two scripts ammoPickup and Recoil which may be shared here once they are finished.

p.s. it is in UnityScript language

Looking forward to your feedback.

#pragma strict
//copyright © 2014 of zakirali786
var bullet : Rigidbody;            //these two variables are used
public var bulletSpeed : int = 20; //to create bullets, bullet speed is low for
								   //testing purposes but can be tweaked in the inspector

var currentAmmo : int = 0;  //keeps track of the current ammo remaining in the clip
public var totalAmmoForClip : int = 40; //describes maximum capacity of bullets in a clip
public var totalAmmoGun : int = 200; //describes maximum total gun ammo
var ammoUsed : int = 0; //keeps track of ammo used in the clip

function Start () {

}

function Update () {
	
	//will start firing if ammo in the clip is greater than 0
	//and the fire button has been pressed
	if(Input.GetButtonDown("Fire1")  currentAmmo > 0) 
	{
		Shooting(); //calls the shooting function below
	}
	
	//will reload the gun only if the total gun ammo is greater than 0,
	//the "Reload" value was manually assigned in the input manager to
	//the keys 'r' and 'space' 
	if(Input.GetButtonDown("Reload")  totalAmmoGun > 0)
	{
		Reload(); //calls the reload function
	}
}

function Shooting ()
{
		//creates the rigidbody from which to insantiate the 'bullet' which is
		//a prefab that needs to be manually assigned in the inspector
		var bulletClone : Rigidbody;
		
		//script is attached to an empty gameobject in front of the gun which holds the spawn
		//point for the bullets, bullets are spawned and then a velocity is added
		bulletClone = Instantiate(bullet, transform.position, transform.rotation);
		bulletClone.velocity = transform.TransformDirection(Vector3.forward * bulletSpeed);
	
		Destroy(bulletClone.gameObject, 1.0); //destroys the bulletClones after 1 second
		currentAmmo--; //decreases current ammo by 1
		
		//creates a prompt to reload your gun if ammo in clip is less than or equal to 0
		if(currentAmmo <= 0  totalAmmoGun != 0)
		{
			currentAmmo = 0; //if by chance the current ammo has a negative value it will reset to 0
			Debug.Log("Reload your gun, press R!"); // <--self explanatory
		
		}
		
		//if the total ammo and current ammo are less than or equal to 0 then prompt the 
		//player to find some ammo
		if(totalAmmoGun <= 0  currentAmmo == 0) 
		{
			totalAmmoGun = 0; //if by chance totalAmmo is negative  it will reset to 0
			Debug.Log("You have run out of bullets find some ammo!");
		}
}

function Reload ()
{
	
	ammoUsed = totalAmmoForClip - currentAmmo; //keeps track of the ammo that's been fired
	
	if(totalAmmoGun >= totalAmmoForClip) //if the totalAmmoGun is greater than or equal to the totalAmmoForClip
	{
		if(currentAmmo > 0  currentAmmo < totalAmmoForClip)
		{
			currentAmmo += ammoUsed;
			totalAmmoGun -= ammoUsed;
		}
		else if(currentAmmo == 0)
		{
			currentAmmo += totalAmmoForClip; //then add the maximum ammount of bullets into currentAmmo
			totalAmmoGun -= ammoUsed;
		}
	}
	
	//executes in the case when your total ammo in the gun is less than the ammo in the clip
	else if(totalAmmoGun < totalAmmoForClip  totalAmmoGun > 0)
	{
		//this section of code prevents a bug where when in low ammo amounts the gun would add
		//the full ammo for the clip and give a negative value to the totalAmmoGun
		//causing all sorts of errors
		
		
		//when the ammo used is greater than the ammo left in the gun
		//need to make sure it doesn't add the maximum total ammo for the clip
		if(ammoUsed >= totalAmmoGun)
		{
			currentAmmo += totalAmmoGun; //adds the last remaining bullets into currentAmmo
			totalAmmoGun -= totalAmmoGun; //sets the total gun ammo to 0
		}
		else if(totalAmmoGun > ammoUsed) //if gun ammo is more than the ammo used then reload as normal
		{
			currentAmmo += ammoUsed;
			totalAmmoGun -= ammoUsed;
		}
	}
}

function OnGUI ()
{
	var ammoString : String = "Ammo: "; //creates an ammo string showing the ammo in the gun
	
	//gui is created in the top left corner showing the current ammo and total gun ammo
	ammoString = GUI.TextField(Rect(10, 10, 200, 20), ammoString + currentAmmo + " : " + totalAmmoGun);
}
1 Like

is this just me or trully your scripts are lots of error