Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 DaUr3 · Jun 23, 2018 at 01:19 AM · charactercharactercontrollerclassesinterfacepolymorphism

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.

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
2
Best Answer

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 {}

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 DaUr3 · Jun 23, 2018 at 04:50 AM 0
Share

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.

avatar image Trevdevs DaUr3 · Jun 23, 2018 at 04:54 AM 0
Share

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

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

127 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

Related Questions

storing scripts that implements an interface 0 Answers

Character controller Jumps while using Locomotion 0 Answers

Background & sprites are smearing towards the player during movement. Help determine the cause and solution? 0 Answers

Changing the landing and switch directions of standard assets TPC controller 0 Answers

How to make my object go in the direction its facing [C#] 3 Answers


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