- Home /
What is the BEST Way to do Character Classes
So In my game I am thinking of allowing the player to change different classes; however, I am beginning to think that the way that I have set up my current class system isn't very flexible.
My current system has a bunch of subclasses that extend from a interface, each player controller will a class variable that is changed over time as the player changes classes, but I've realized that if I want to implement something like a dodge roll, I would have to get the capsule collider of the player object, which is very hard to do with the current system.
I was thinking of doing something like this:
Class playerClass = new Class(this);
// Copy the playerController object into each class object
So I could get full access to all of the variables, but to me it doesn't seem like a good idea.
How should I get about implementing a character class system that would allow for full flexibility of implementing various things? How do other games like TF2 or Overwatch do this?
Thanks.
Answer by Trevdevs · Jun 23, 2018 at 02:22 AM
There is no "Best" way to code anything thats the beauty of programming it doesn't matter how you do it as long as it works, and most importantly you understand how it works.
Option 1 "Inheritence Method"
1 Class called "Class" with a subclass for each class
ex. public class Mage : Class
Option 2 "Enum method"
public class Player : MonoBehaviour { public enum classType {Mage, Warrior, Archer}; public classType playerClass;
}
Option 3 "ScriptableObjects" (I'm guilty of using this a lot more than I probably should)
1 parent class Public class Class : ScriptableObject {};
as many as you want subclasses [CreateMenuAsset(menuName = "Class/Mage")] public class Mage : ScriptableObject {}
I'm just curious though, if I would need to access the CapsuleCollider of the player in one of these classes how would I do it without copying the playerController behavior into each CharacterClass script.
Well since only option 2 is an actual monobehaviour itself that'd be the simpliest to do just throw it on a object with a collider and reference it in the class.
The other 2 you need a second script think of option 1 and 3 as methods for storing the variables and methods of each kind of playerclass
Lets say you use the scriptableobject one your code would look something like this
public class Class : ScriptableObject
public class $$anonymous$$age : Class
{
public int health;
public int speed;
}
public class Player : $$anonymous$$onoBehaviour
{
public CapsuleCollider collider; //Assign your collider here
public Class class; //Assign your instance class to mage here
}
Your answer
Follow this Question
Related Questions
2D sprite rotating in 3D plane 0 Answers
Player inversed inputs 0 Answers
second character controller ??? 4 Answers
Help with animation... Please 2 Answers
Character Interface Change (Help) 0 Answers