- Home /
Making a Trait Mechanic?
I have always loved The Sims 3, and one of my favourite little things about that game is that you can give your character personality traits. For example, you could make them silly, evil and a kleptomaniac. If I wanted to make my own trait system, how can I do something like change their speed or health or something based on a few traits that a player selects? This must be a big question, but I have some idea that I should be using classes?
Yes, it's a big question, and it's not obvious that this is even a Unity question.
Answer by Dazdingon · Jun 06, 2014 at 03:37 PM
You could try a very simple system that adds stats together :
// Have a script add and remove traits from your players's playerTraits
var playerTraits : TraitBonus[] = new TraitBonus[3];
// Keep your original stats unaffected
var originalStats : PlayerStats;
// Use playerStats for the game culculations
var playerStats : PlayerStats;
class PlayerStats {
var health : float = 0f;
var attack : float = 0f;
var defence : float = 0f;
var speed : float = 0f;
function AddTrait (bounus : TraitBonus) {
health += bounus.health;
attack += bounus.attack;
defence += bounus.defence;
speed += bounus.speed;
}
function MakeInstanceOf (stats: PlayerStats ) {
health = stats.health;
attack = stats.attack;
defence = stats.defence;
speed = stats.speed;
}
}
class TraitBonus {
var traitName : String = "Unknown";
var health : float = 0f;
var attack : float = 0f;
var defence : float = 0f;
var speed : float = 0f;
}
When your player's playerTraits changes, add them to an instance of his originalStats
function ApplyTraitsToStats () {
playerStats.MakeInstanceOf(originalStats);
for (trait in playerTraits) {
playerStats.AddTrait(trait);
}
}
Use the inspector to set up available Traits, then store the references in playerTraits[]
This is totally fucking awesome! Very advanced for me, but I'm going to try analyzing it! Thanks!