Hello, I’m making a strategy game and ran into some class hierarchy issues. I constructed a character class and put in most values used for a character object and any basic move or attack functions in it. From there I made two child classes called FriendlyCharacter and EnemyCharacter. In those update functions I handled basic selection and how those characters would set target/target position. I have a game object in the scene with the FriendlyCharacter script attached and another game object with the EnemyCharacter script on it.
Now I want to implement soldier classes for characters such as warrior, archer, etc. These classes will have different weapons and fighting stances. The problem I am running into now is how to deal with inheritance since both use information from Character class (not necessarily everything). The structure I imagine goes as follows, base character class with soldier class children-> Warrior : Character or Archer : Character. From there each soldier class can be either friendly or enemy. The code below covers most of what is involved with my issue.
Example Character.cs
public class Character : MonoBehavior {
public int Health {
get;
set;
}
// ...
virtual void SetTarget() {
// ...
}
}
Example FriendlyCharacter.cs
public class FriendlyCharacter : Character {
void Start() {
anim = GetComponent<Animator>();
rend = GetComponent<Renderer>();
// ...
}
void Update() {
if (inputFromMouse)
SetTarget();
// ...
}
override void SetTarget() {
// ...
}
}
Example Warrior.cs
// What I had in mind
public class Warrior : Character {
public enum Stances {
Default,
BlockLow,
BlockHigh
}
private Stances mStance;
public Stances Stance {
get {return mStance;}
set {mStance = value;}
}
protected Shield shield;
protected Animator shieldAnim;
protected Renderer shieldRend;
void Start() {
Health = 125;
// ...
shield = transform.GetChild(1).GetComponent<Shield>();
shieldAnim = shield.GetComponent<Animator>();
shieldRend = shield.GetComponent<Renderer>();
// ...
}
void Update() {
if (input) {
Stance = Stances.BlockLow
BlockLowStance();
}
// ...
}
void BlockLowStance() {
// ...
}
}
I’m currently trying to implement the Warrior class but can’t figure out a way how. I feel like I have two options:
FriendlyCharacter Primary Script (Way I have now)
- Issue is accessing the right class functions, I wouldn’t be able to initialize object with right values unless I create instantiate all versions of my soldier classes and assign the correct one to current class.
Warrior Primary Script
- Issue is how to approach my update function. I couldn’t access the proper functions for friendly or enemy per update.
Can someone help on structuring this correctly?