Character Class Hierarchy with soldier classes and friendly/enemy character

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?

If it was me then rather than having two different classes for friendly and enemy character, I would just use a boolean to decide if it is friendly or enemy character in your base Character class itself. If you expect to have some other types then an enum can be used.

Now you can have your different classes like warrior or archer to derive from you Character directly and then you can set these characters to be friendly or enemy. It it better than having to create two versions of each character type (one for enemy and one for friendly).

Also not sure what you mean by accessing Update method but if you want to have ability for your child classes to be able to access the Monobehaviour methods like Start and Update then you can set these methods to be virtual in your base class and then you can override these methods in your child classes.