Scriptable Weapons For The player

Stream line your weapon creation. This is a work in progress, needs scriptable bullets and support for 3d models of weapons. Along with a lot of other things.

What you need:

  • Player GameObject(can just be a cube), with a PlayerWeaponController Script Component

  • A WeaponStats Script to create the weapon variations

  • Some type of bullet GameObject with a PlayerBullet Script Component on it

  • PlayerWeaponController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerWeaponController : MonoBehaviour {

	public KeyCode nextWeapon;
	public WeaponStats[] weaponStats;
	public int currentWeapon = 0;
	// TODO later add scritable bullets
	// TODO get ammo from a player inventory
	public int ammo = 100;
	public float reloadTimer;
	public float firingTimer;


	// Use this for initialization
	void Start () {
		// TODO do I need a weapon setup method
	}


	// Update is called once per frame
	void Update () {
		// TODO add prev weapon and button to jump straight to a weapon
		if(Input.GetKeyUp(nextWeapon)){
			ChangeCurrentWeapon();
		}


		// TODO add check for special firing modes, ex burst uses 3 bullets
		// check for button press, then check if enough ammo
		// TODO add clip reload timer check
		if(Input.GetButton("Fire1") && ammo > 0 && firingTimer < Time.time){
			// Debug.Log("Pushed Fire");
			DecrementAmmo();
			Shoot();
		}
	}

	void ChangeCurrentWeapon(){
		// increment current weapon
		currentWeapon++;
		// if out of range loop
		currentWeapon = currentWeapon % weaponStats.Length;
	}

	void DecrementAmmo(){
		ammo --;
		if(ammo < 0){
			ammo = 0;
		}
	}

	// Shoot
	public void Shoot (){
		// TODO add reloadTimer
		if(firingTimer < Time.time){
			resetFiringTimer(weaponStats[currentWeapon].fireRate);
			// TODO add multiple firing and accuracy recoil
			Instantiate(weaponStats[currentWeapon].bullet, transform.position, transform.rotation);
		}

	}

	////////// TIMERS //////////
	// add the cooldown time to current time
	private void resetReloadTimer(float reloadTime){
		reloadTimer = Time.time + reloadTime;
	}

	private void resetFiringTimer(float firingTime){
		firingTimer = Time.time + firingTime;
	}
}

  • WeaponStats
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu (menuName = "PluggableWeapons/WeaponStats")]
public class WeaponStats : ScriptableObject {

	// time to reload clip
	public float reloadSpeed = 1;
	public int bulletsPerClip = 9;
	// amount of damage caused, TODO could add positional damage(where target was hit)
	public int damage = 35;
	// time between firing
	public float fireRate = 1;

	public GameObject bullet;

	// TODO firing modes, single, semi auto, burst, full auto...
	[Header("Firing Modes")]
	public bool singleFireMode = true;
	public bool semiAutoFireMode = false;
	public bool burstFireMode = false;
	public bool fullAutoFireMode = false;
	//Enum??
	// price, color, sound...
}

  • PlayerBullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// TODO make pool

public class PlayerBullet : MonoBehaviour {

	public float lifeTime = 10f;
	public float speed = 1f;

	// Use this for initialization
	void Start () {
		Destroy(gameObject, lifeTime);
	}

	// Update is called once per frame
	void Update () {
		transform.Translate(0, 0, speed * Time.deltaTime);
	}
}

  • Vector Maths - Unity Official Tutorials

    Vector Maths - Unity Official Tutorials