Player Controller Design (Modular vs All in one)

I have some question that can be both simple and complex. I know this has probably been asked before but I figured I would ask to try to gain a better understanding.

I would like to build a player controller that I can use with also understand the logic behind it.

I am unsure if I should build it all together or separate it out into multiple parts.

What are some of the reasons I would want to keep it together vs separating it?

Is there anything I should keep in check with separating the logic?

I am also wondering how do actual game studios do this do they usually separate the logic of the player controller into separate scripts or build on script with most of the logic?

Any answers to my questions would be great!

Thank you in advance!

Separate what exactly?

You should definitely separate the input event handling and pre-processing (eg handle mouse movement delta vs gamepad absolute motion), the player controller statemachine, and the statemachine transition events then cause animation and ragdoll updates, audio playback, vfx, and so forth. Some of that you can also trigger with the Animator.

If you mean camera motion, then no. Nobody (hopefully) scripts the camera anymore. You’d plug in Cinemachine and set it up with zero or minimal scripting and it covers 97% of all game’s needs in AAA quality.

If you mean to separate motion and rotation, then no. Separating different controller states like wall-running, jumping, ladder-climbing and so forth? Probably not, they would have to become very complex before that makes sense.

Separate all the repeated code you’ll need in every kind of step along the way? Definitely! That’ll include a lot of simple trigonometry and math calculations, so that’s mostly utility methods you want to keep separate for consistency and to reduce code complexity/readability.

You are assuming that everyone builds their own character controller. While this may be true, it’s also one of the things that constitutes re-inventing the wheel all over the place.

I hope (but know this isn’t the case) many teams actually adopt a well-established, proven character controller and merely adapt them to their needs, perhaps over time taking over complete ownership if and where that makes sense.

Say you want to build a run-of-the-mill 1st/3rd person or top-down hack-n-slash game then there’s existing character controllers that are perfectly adequate to utilize even if you have special/unusual controller mechanics. It would be absolutely wasteful to spend the 4-8 weeks to get to the same level of playability if you can use something that already exists, especially given the cost being far less than $100. Or FREE.

Just like with any script you write - you typically start by writing a single script and if it begins to grow in size where it starts to become cumbersome to navigate and difficult to understand then you can split it up into smaller scripts.

Although we all have different levels of experience and different styles of coding. Some coders can create a script in 50 lines that others coders may struggle to keep below 250 lines. A smaller script isn’t necessarily easier to understand but they are easier to navigate and quicker to read.

A great example of a player controller that should be multiple scripts is a game like Tomb Raider where the character has totally different control schemes - like walking vs dangling from a ledge. In this scenario the two modes can be handled by two independent scripts that enable each other when appropriate.

But an FPS game where the only options are to walk, run and crouch?. This could easily be done in a single script because the only real difference between those modes is the speed.

This helps a bunch, I did not really think about studios simply adapting an already working character controller this way it’s like you said not reinventing the wheel. :smile:

Also sort of forgot about Cinemachine will have to use this moving forward. Need to quickly relearn it again though.

I did built a really simple character look script but I can simply add Cinemachine to this would be way easier this way.

Thank you for the reply!

This also helps as well, it made me think of instead of a player controller all in one tbh for me it would be easer to split it a part because it’s way easer to understand what’s happening also if something goes wrong I will know exactly, usually exactly, where the issue is.

Really It would be better to think of the player controller more like a state machine, you can split it apart into separate scripts or states depending on what you want added to your player controller or not.

I think for this approach or the one I need, it’s best for a modular system. So if I need something like moving I can simply add it to a character, then if I later need the character to run I can simply add this and it will just work without having to think about modifying the movement script. Or if I wanted something like jumping or crouching I could simply drag the scripts with this logic adding it to the player and it will just work.

Thank you for the help!