Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MooseCadets · Feb 10, 2017 at 03:33 AM · c#classespolymorphism

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by HarshadK · Feb 10, 2017 at 07:15 AM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MooseCadets · Feb 10, 2017 at 07:20 AM 0
Share

Yeah I tried doing that for a bit, although when I was putting the methods from both over in the character class I was worried it was getting excessive. That might be my best option though, I'll try to commit to it and see how it works. Thanks

avatar image MooseCadets MooseCadets · Feb 10, 2017 at 08:09 AM 0
Share

I just finished moving everything over. This works great, and the extra long Character class is well worth it for how easy it is to manage on the scene manager and child class. Thanks a lot.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

281 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Polymorphism trouble: can't cast child to parent 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Handling different combinations of enum selection 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges