Sandbox Logo
Page Header

Player Controller

I started looking at making our current character controller able to stand and move with platforms, and get pushed around. The current controller works very much like the HL1/2 controller, in that it traces a box around the world and has no "real" physical presence.

So I got about 30 minutes into this and realised I was basically emulating a physics engine and it was a huge hack. It would be better if we could be physics with a character controller hacked in, rather than a character controller with physics hacked in.
The rigidbody for the player controller is a bit of a weird shape. It's a box at the bottom and a capsule at the top.
The box is used for the feet. It has high friction. Using a box here instead of a capsule makes it feel like the hl2 player controllers. You don't sink when walking near an edge etc.

The body is wider than the feet, and has no friction. This means that when you graze a wall it doesn't slow you down. This is important when jumping against walls, you don't want the wall to grab you and be unable to jump.
Because the player has weight, they can push things, and those things react as they should.


It's just a physics object so it can sit on a platform and move along with it like any other. We do some calculations under the hood to give you eye rotation and ground velocity.

The controller pays attention to the ground friction, so things like ice are possible.

Full ducking support is built in. It won't unduck until it has room to.
It also fully supports duck jumping too. When it duck jumps it moves the body upwards so the eye position stays in the same place, and allows you to get to higher places than just regular jumping.


The controller can have different move modes. It can be extended with your own move-modes easily. We include a ladder movement, which detects a ladder trigger. (we're working on climb animations)


We also included a swimming move mode by default, which is enabled by being inside a water trigger. The player does a little jump when leaving the water to help them get to the edge.

I loved the crushing stuff in Half-Life. Scientists being gibbed by a door was awesome. So I want the same thing to be possible in s&box. I've done a bunch of work towards this, but still want something more generic, so it's built into the physics engine.

You can get pushed by other physics objects, and react how you would expect.

Stepping up and down is an important part of any character controller

There's a built in camera controller. It's a first person and third person camera. The third person traces backwards to avoid clipping through walls.
You can turn this feature off by right clicking the camera tab, and create your own camera controller in another component.
The built in animator will take care of rotating and setting animgraph variables on the player model. It can also play footstep sounds automatically.
You can also turn this off and code your own animator in another component.
The built in input feature sets the player's wish velocity, duck wish state and jump. It also has pressing built in, allowing the player to automatically interact with any component that implements the IPressable interface (like buttons, doors etc).
You can also turn this off and code your own input system in another component.
I like this implementation. I have been weary in the past of doing too much, providing functionality that developers should be implementing themselves. But I think this is the right thing. It's got an easy mode, for the 90%.. where you don't have to do anything and it's good enough. Then it's got a hard mode for the 10% that know what they're doing and want more.

It has turned making a player controller with all these features from "download this library, create this, do this, press this, write this" - to just two clicks.

Comments

Jan, 1 2024
465
that's pretty useful
Oct, 10 2023
130
pretty good
Apr, 4 2021
225
Amazing!
Jul, 7 2024
0
Could a cylinder be a viable solution instead of capsule + box? It would make head jumping other players easier which could be fun
Jul, 7 2023
115
Nice stuff!
Apr, 4 2021
650
Yeah a cylinder would be nice, but have to be careful not to add a "lip" or the player will get caught up on edges when falling
Feb, 2 2024
95
i tried it today and i can say that it is the best player controller out of the box
Aug, 8 2023
50
Woah!
Mar, 3 2023
120
Very cool
Nov, 11 2024
0
will you get sent flying if youre going fast while noclipping and stop noclipping (like in gmod)
Apr, 4 2023
0
This is Amazing.
Aug, 8 2022
60
real
Nov, 11 2024
50
good stuff :)
Jul, 7 2023
0
Great job Garry, but there’s an issue that needs improvement in my opinion. When pressing ‘crouch’, the camera makes a very abrupt height change.
Sep, 9 2022
15
Thank you Gary very cool, im definitely in the percentage of people who prefer to not spend a bunch of time coding a player model, especially when things are constantly changing in an alpha causing code to break
May, 5 2021
5
this is game changing for simplifying the development process of source style games
Nov, 11 2023
0
please add more graphics setting, like ao(if it use), shadow map quality, textures quality (if is possible to change) and a level of detail