Okay so I have been thinking on this and am not sure at all how to go about this. Basically I want to make weapons types. Like SMG, Assault Rifle, Sniper Rifle, Pistol, Shotgun, and Launcher. I also wanna be able to make a leveling system to where if I kill an enemy with lets say an Assault Rifle, to actually have the experience that is gained after the kill to go to the Assault Rifle skill. I have TRIED alot of different ways, but I think I need someone that is a bit more experienced to help me here. The leveling system for weapons will be JUST like in Borderlands with the weapon proficiencies. If anyone would have an idea of how to sort out the weapon categories, and to check which type of weapon is currently equipped. Any help is greatly appreciated.
3 Answers
3Create an enum with all of the types and then you can use a switch statement to do the leveling.
//WeaponTypes.cs
enum WeaponTypes
{
SMG,
AssaultRifle,
SniperRifle,
Pistol,
Shotgun,
Launcher
}
//In Your Gun Class
//You can edit this in the Unity Editor
public WeaponTypes weaponType;
//Leveling Code
void LevelUp()
{
switch(weaponType)
{
case WeaponTypes.SMG:
//level up SMGs
break;
case WeaponTypes.AssaultRifle:
//level up Assualt Rifles
break;
case WeaponTypes.SniperRifle:
//level up Sniper Rifles
break;
case WeaponTypes.Pistol:
//level up Pistols
break;
case WeaponTypes.Shotgun:
//level up Shotguns
break;
case WeaponTypes.Launcher:
//level up Launchers
break;
}
}
This example can easily be expanded to do anything you need.
There’s probably a few ways, but I think the most straight forward one would be to simply create tags and assign them to each weapon type, then you can manage their stat changes via calling those tags.
Here’s an official tutorial on the subject: Unity Connect
I’d use an enum. The function that accumulates points when a kill is made would then only need the values of the enum and the points earned. It could then tally the points into the appropriate bucket. Whenever you wanted to display a point value, just request the value for a given enum.
To make this happen, all you need is a public enum, a dictionary, and two functions, AddPoints and GetPoints.
Updated with code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum WeaponClass
{
Pistol,
SMG,
AssaultRifle,
SniperRifle,
Shotgun,
Launcher
}
[System.Serializable]
public class Stats
{
private Dictionary<WeaponClass, int> weaponPoints;
public void AddPoints(WeaponClass weaponClass, int points)
{
if (weaponPoints == null)
weaponPoints = new Dictionary<WeaponClass, int>();
if (weaponPoints.ContainsKey(weaponClass))
weaponPoints[weaponClass] += points;
else
weaponPoints.Add(weaponClass, points);
}
public int GetPoints(WeaponClass weaponClass)
{
return weaponPoints.ContainsKey(weaponClass) ? weaponPoints[weaponClass] : 0;
}
}
As a bonus, you can de/serialize this class directly to/from disk.
Not at all. I would have posted a code example to begin with, but I was using my phone. The email I received when you posted your comment simply reminded me to do so. :)
– iwaldropThis is off topic, but I'm trying to build connections with programmers in the gaming industry. I don't know any Unity Developers. Do you mind if I pick your brain a little bit? If yes, I'll give you my email.
– Spinnernicholas
Possible to translate this to a javascript alternative?
– cwaite84All you have to do to make it javascript is change the function header to: function LevelUp() and change the variable declaration to: var weaponType: WeaponTypes; That should do it, but let me know.
– Spinnernicholas