Hi, everyone! ![]()
I just wanted to add to the pool some code that could make someone’s life a little easier; since I got so much information from these forums over the years. This script is for weapon switching with the mouse scroll wheel, and hot-keys 1-4. It was wrote some more could easier be added to it. It has stats for the weapons, and a fully functional reload system with hot-key “r”.
I wrote added some OnGUI code to display the current amount and current ammo in the magazine; along with the weapon selected. The only problem with the script is that the shotgun does not have random spread coded in yet, but it still works. It should be commented enough so that it is easy for any one to follow and change where they see fit.
I will update later when i finish the Shotgun spread. Oh and the rifle is setup up to be automatic, while everything else is semi-auto (Per click; according to the fire rate for each weapon).
Updated: Fixed a bug; and added some Shotgun random spread (only on the z axis, though).
//PreFabs--------------------------------------------------------------
var bullet : GameObject;
var shotgun: GameObject;
var rocket: GameObject;
private var bulletSpawnPoint: GameObject;
//Misc-----------------------------------------------------------------
var AttackTimer : float;
var waitingForReload: float;
var shootDirection: Vector3;
var currentWeapon: int;
var previousWeapon: int;
//Booleans--------------------------------------------------------------
var weaponSwitched: boolean;
var isReloading: boolean;
var weaponSetup: boolean;
var hasPistol: boolean;
var hasRifle: boolean;
var hasShotgun: boolean;
var hasRocket: boolean;
//Weapon Reload Times; lower means faster--------------------------------
private var pistolReloadTime:float = 1.5f;
private var rifleReloadTime:float = 3f;
private var shotgunReloadTime:float = 3f;
private var rocketReloadTime:float = 5f;
private var reloadTimer: float; //Container for current Max Ammo
//Weapon Fire Rates; lower is faster-------------------------------------
private var pistolFireRate:float = .25f;
private var rifleFireRate:float = .1f;
private var shotgunFireRate:float = .5f;
private var rocketFireRate: float = 0;
private var fireRate : float; //Container for current Max Ammo
//Weapon Max Clip size---------------------------------------------------
private var maxPistolClipSize: int = 8;
private var maxRifleClipSize: int = 30;
private var maxShotgunClipSize: int = 6;
private var maxRocketClipSize: int = 1;
private var maxClipSize: int; //Container for current Max Clip size
private var pistolCurrentInClip: int;
private var rifleCurrentInClip: int;
private var shotgunCurrentInClip: int;
private var rocketCurrentInClip: int;
var currentInClip: int; //Container for ammo in clip; will go to GameController
//Weapon Max Current Ammo----------------------------------------------
private var maxPistolAmmo: int = 80;
private var maxRifleAmmo: int = 330;
private var maxShotgunAmmo: int = 30;
private var maxRocketAmmo: int = 5;
private var currentPistolAmmo: int;
private var currentRifleAmmo: int;
private var currentShotgunAmmo: int;
private var currentRocketAmmo: int;
var currentAmmo:int;//Container for current Ammo; will go to GameController
//Weapon Damage-----------------------------------------------------------
private var pistolDamage: int = 5;
private var rifleDamage: int = 3;
private var shotgunDamage: int = 7;
private var rocketDamage: int = 15;
static var weaponDamage: int; //Container for current weapon damage
//Bullet Speed------------------------------------------------------------
private var pistolBulletSpeed: float = 15;
private var rifleBulletSpeed: float = 20;
private var shotgunBulletSpeed: float = 15;
private var rocketBulletSpeed: float = 10;
static var bulletSpeed: float; //Container for current bullet speed
function Start()
{
//Get bullet spawn point
bulletSpawnPoint = GameObject.Find("BulletSpawnPoint");
//Temp seetings for testing, CHANGE LATER!!!!
hasPistol = true;
hasRifle = true;
hasShotgun = true;
hasRocket = true;
//Setup current and previous weapon
weaponSetup = false; // for first time setup
isReloading = false;
weaponSwitched = false;
currentWeapon = 0; // for first time setup
currentPistolAmmo = maxPistolAmmo;
currentRifleAmmo = maxRifleAmmo;
currentShotgunAmmo = maxShotgunAmmo;
currentRocketAmmo = maxRocketAmmo;
pistolCurrentInClip = maxPistolClipSize;
rifleCurrentInClip = maxRifleClipSize;
shotgunCurrentInClip = maxShotgunClipSize;
rocketCurrentInClip = maxRocketClipSize;
}
function Update ()
{
//First time setup, runs once.
if(!weaponSetup)
{
GetCurrentWeapon(currentWeapon);
weaponSetup = true;
}
//Change weapon with Mouse wheel
if(Input.GetAxis("Mouse ScrollWheel") > 0)//MouseWheel button up
{
previousWeapon = currentWeapon;
weaponSwitched = true;
if(currentWeapon == 3)
{
currentWeapon = 0;
}
else
{
currentWeapon++;
}
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0)//Mouse Wheel down
{
previousWeapon = currentWeapon;
weaponSwitched = true;
if(currentWeapon == 0)
{
currentWeapon = 3;
}
else
{
currentWeapon--;
}
}
//Change weapons with Numbers directly; 1-4.
if(Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1) hasPistol)
{
previousWeapon = currentWeapon;
weaponSwitched = true;
currentWeapon = 0;
}
else if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2) hasRifle)
{
previousWeapon = currentWeapon;
weaponSwitched = true;
currentWeapon = 1;
}
else if (Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.Keypad3) hasShotgun)
{
previousWeapon = currentWeapon;
weaponSwitched = true;
currentWeapon = 2;
}
else if (Input.GetKeyDown(KeyCode.Alpha4) || Input.GetKeyDown(KeyCode.Keypad4) hasRocket)
{
previousWeapon = currentWeapon;
weaponSwitched = true;
currentWeapon = 3;
}
//Input for Reload
if(Input.GetButtonDown("Reload") currentAmmo > 0 currentInClip != maxClipSize)
{Debug.Log("test4");
Reload();
}
//Did we switch weapons, if so save weapon stats
if(weaponSwitched)
{
SaveCurrentWeaponsStats(previousWeapon);
GetCurrentWeapon(currentWeapon); //What is the current weapon selected?
weaponSwitched = false;
}
//if can fire, fire. Check for reloading!
if(!isReloading)
{
if (AttackTimer > 0)
{
AttackTimer -= Time.deltaTime;
}
if (AttackTimer <= 0)
{
if(currentWeapon == 1)
{
if(Input.GetButton("Fire"))//AutoFire bullets
{
Fire(currentWeapon);
AttackTimer = fireRate;
currentInClip--;
if(currentInClip == 0 currentAmmo >= 1)
{Debug.Log("test1");
Reload();
}
}
}
else
{
if(Input.GetButtonDown("Fire"))//Single fire bullets per click
{
if(currentInClip >= 1)
{Debug.Log("test2");
Fire(currentWeapon);
AttackTimer = fireRate;
currentInClip--;
if(currentInClip == 0 currentAmmo >= 1)
{Debug.Log("test3");
Reload();
}
}
}
}
}
}
else
{
if (AttackTimer > 0)
{Debug.Log("test5");
AttackTimer -= Time.deltaTime;
}
waitingForReload -= Time.deltaTime;
if(waitingForReload <= 0)
{
isReloading = false;
}
}
}
function GetCurrentWeapon(currentWeapon: int)
{
Debug.Log("Change to new weapons and set stats!");
//Setup max ammo; clip size; fire rate; damage based off of weapon
switch (currentWeapon)
{
case 0://Pistol
reloadTimer = pistolReloadTime;
bulletSpeed = pistolBulletSpeed;
weaponDamage = pistolDamage;
currentAmmo = currentPistolAmmo;
fireRate = pistolFireRate;
maxClipSize = maxPistolClipSize;
currentInClip = pistolCurrentInClip;
break;
case 1://Rifle
reloadTimer = rifleReloadTime;
bulletSpeed = rifleBulletSpeed;
weaponDamage = rifleDamage;
currentAmmo = currentRifleAmmo;
fireRate = rifleFireRate;
maxClipSize = maxRifleClipSize;
currentInClip = rifleCurrentInClip;
break;
case 2://Shotgun
reloadTimer = shotgunReloadTime;
bulletSpeed = shotgunBulletSpeed;
weaponDamage = shotgunDamage;
currentAmmo = currentShotgunAmmo;
fireRate = shotgunFireRate;
maxClipSize = maxShotgunClipSize;
currentInClip = shotgunCurrentInClip;
break;
case 3://Rocket
reloadTimer = rocketReloadTime;
bulletSpeed = rocketBulletSpeed;
weaponDamage = rocketDamage;
currentAmmo = currentRocketAmmo;
fireRate = rocketFireRate;
maxClipSize = maxRocketClipSize;
currentInClip = rocketCurrentInClip;
break;
default:
Debug.Log("There is no current weapon!");
break;
}
}
function SaveCurrentWeaponsStats(previousWeapon: int)
{
Debug.Log("Saved current weapon stats.");
switch (previousWeapon)
{
case 0://Pistol
currentPistolAmmo = currentAmmo;
pistolCurrentInClip = currentInClip;
break;
case 1://Rifle
currentRifleAmmo = currentAmmo;
rifleCurrentInClip =currentInClip;
break;
case 2://Shotgun
currentShotgunAmmo =currentAmmo;
shotgunCurrentInClip =currentInClip;
break;
case 3://Rocket
currentRocketAmmo = currentAmmo;
rocketCurrentInClip = currentInClip;
break;
default:
Debug.Log("There was no previous weapon!");
break;
}
}
function Fire(currentWeapon: int)
{
Debug.Log("Fired something");
switch (currentWeapon)
{
case 0://Pistol, this is good.
shootDirection = gameObject.GetComponent("PlayerMoveController").motor.facingDirection.normalized;
Instantiate(bullet, bulletSpawnPoint.transform.position, Quaternion.LookRotation(shootDirection));
break;
case 1://Rifle, might be ok.
shootDirection = gameObject.GetComponent("PlayerMoveController").motor.facingDirection.normalized;
Instantiate(bullet, bulletSpawnPoint.transform.position, Quaternion.LookRotation(shootDirection));
break;
case 2://Shotgun, increase i for more bullets and bigger spread
shootDirection = gameObject.GetComponent("PlayerMoveController").motor.facingDirection;
for(i=0; i<6; i++)
{
shotgunSpread =(Vector3(0,0,i) + shootDirection).normalized;
Instantiate(shotgun, bulletSpawnPoint.transform.position, Quaternion.LookRotation(shotgunSpread));
}
break;
case 3://Rocket, need to change!!
shootDirection = gameObject.GetComponent("PlayerMoveController").motor.facingDirection.normalized;
Instantiate(rocket, bulletSpawnPoint.transform.position, Quaternion.LookRotation(shootDirection));
break;
default:
Debug.Log("There is a problem with firing!");
break;
}
}
function Reload()
{
isReloading = true;
waitingForReload = reloadTimer;
if(currentAmmo >= maxClipSize)
{
var temp2: int = maxClipSize - currentInClip;
currentAmmo -= temp2;
currentInClip += temp2;
}
else
{
if(currentInClip + currentAmmo > maxClipSize)
{
var temp: int;
temp = (currentInClip + currentAmmo) - maxClipSize;
currentInClip = maxClipSize;
currentAmmo = temp;
}
else
{
currentInClip += currentAmmo;
currentAmmo = 0;
}
}
}