Templating a Falloutish type of Setup

sickb0y247 Posts: 11Member
Hey guys, thought I'd post this up here in case it might help somebody. This is my work around the inventory system to make more of a dynamic Fallout type of setup with the weapons and ammo. I can cycle through multiple weapons of the same type while keeping their differences. You can change the capacity and reload speeds of your various weapons in the editor while the game is running and it will reflect it the next time you equip that particular weapon.

The only change to UFPS code you'll need to do is add a string variable to the vp_UnitBankType script. I named mine "nameInScene" which you can see below. You'll need to go through each weaponType you have in the editor and put the name of the game object it will be referencing in your scene. "1Pistol", "2Shotgun", ect...

Then just drag and drop the UnitBankTypes into this script and it should work.

This is set up with Hero HD Weapons prefab but it should work with any player setup as long as you can make it reference the in-scene game object that your weapon needs to reference. Or if you don't care about manipulating your animations and such, don't bother with it.

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

[System.Serializable]
public class Weapon{
public vp_UnitBankType weaponType;
public int id;
public vp_UnitType ammoType;
public Animation animations;
public GameObject blueprint;
public float reloadSpeed = 1.0f;
public int capacity = 10;

public Weapon(vp_UnitBankType type){
weaponType = type;
id = vp_Utility.UniqueID;
ammoType = type.Unit;
// get the object created in scene that references this weapon
blueprint = GameObject.FindWithTag("MainCamera").transform.FindChild(type.nameInScene+"Transform").FindChild(type.nameInScene).gameObject;
// get the animations for that object
animations = blueprint.transform.GetComponentInChildren<Animation>();

setCapacity (capacity);
setReloadSpeed (reloadSpeed);
}

public void setReloadSpeed(float speed){
reloadSpeed = speed;
setReloadAnimationSpeed ();
}

public void setReloadAnimationSpeed(){
//get the name of the reload animation
vp_FPWeaponReloader script = blueprint.GetComponent<vp_FPWeaponReloader>();
string animName = script.AnimationReload.name;

animations [animName].speed = reloadSpeed;
script.ReloadDuration = 1; // This will need to be tailored based on the length of the animation and how slow or fast you are making it
}

public void setCapacity(int cap){
capacity = cap;
weaponType.Capacity = cap;
}
}

public class ReferenceScript : MonoBehaviour {

public vp_Inventory theInventory = null;
public List<Weapon> weaponList = new List<Weapon> ();

public vp_UnitBankType pistolType;
public vp_UnitBankType ARType;
public vp_UnitBankType shotgunType;

private int indexCycler = 0;

void Awake ()
{
theInventory = GameObject.FindObjectOfType(typeof(vp_Inventory)) as vp_Inventory;
Invoke ("createArsenal", 1);
}


void Update ()
{

// add ammo of currently equipped weapon to inventory
if (Input.GetKeyDown (KeyCode.K)) {
theInventory.TryGiveUnits(theInventory.UnitBankInstances[0].UnitType, 20); // add bullets to current weapon
Debug.Log(theInventory.GetUnitCount (theInventory.UnitBankInstances[0].UnitType)); //get current weapon bullet count (not loaded)
}

// cycle through your weapons
if (Input.GetKeyDown (KeyCode.O)) {
if (indexCycler < weaponList.Count-1)
indexCycler++;
else
indexCycler = 0;

equipWeapon(weaponList[indexCycler]);
}
}

public void createWeapon(vp_UnitBankType type){
Weapon weapon = new Weapon (type);
weaponList.Add (weapon);
}

public void equipWeapon(Weapon weapon){
if (theInventory.UnitBankInstances.Count > 0)
removeWeapon ();

weapon.setCapacity (weapon.capacity);
weapon.setReloadSpeed (weapon.reloadSpeed);
theInventory.TryGiveUnitBank (weapon.weaponType, 0, weapon.id);
}

public void removeWeapon(){
theInventory.TryGiveUnits (theInventory.UnitBankInstances[0].UnitType, theInventory.UnitBankInstances[0].Count); // put loaded ammo back in the ammo dump before removing weapon
theInventory.TryRemoveUnitBank (theInventory.UnitBankInstances[0]);
}

public void createArsenal(){
createWeapon (pistolType);
createWeapon (ARType);
createWeapon (shotgunType);
createWeapon (shotgunType);
createWeapon (ARType);
}
}

Comments

Sign In or Register to comment.