Creating An Advanced 2D Player Controller Without Any Unity Components! (Part 1)
Today I‘m rebuilding the player controller I created recently, to create a fully-independent, modular, & complete 2D player controller.
Today’s Objective: Outline the design for my completely custom 2D side-scroller player character controller which has NO other Unity component dependencies.
I will be showing how I created this in the following daily posts.
The Aim:
The aim of this development process is to make a custom side-scroller player controller that follows some simple guidelines:
- No dependancies: The controller should have no requirements of other Unity components. No colliders, no rigidbodies. Only itself (and a Transform, because it’s still a GameObject).
- Complete: The controller should, as much as I can imagine at this time, be a complete 2D solution for player movement suitable for almost any situation I may need it for.
- Clean/Uncluttered: While the controller is aiming to be complete, it should not have any unnecessary code or features that don’t have value in most games.
- Customizable/Designer-Friendly: The component should use the Unity Inspector to allow for customization of the important parts of the component’s behaviour. This should be simple/informative enough to allow someone else to get it working perfectly without me.
The Features:
Since I am going to the trouble of making this from scratch, there are a number of features I want to include in the controller for the current and future projects:
- Horizontal movement: Standard sideways movement.
- Acceleration/Deceleration: The player should gradually speed up and slow down at either end of a horizontal movement.
- Sprinting/Running: Standard faster movement while left shift is held down.
- Jump ability: Standard player ability.
- Air-jump/multi-jump: The player should be able to optionally jump again while airborne, a limited number of times.
- Collisions/No overlaps: Player should be able to stand on “ground” and not pass through physical objects.
- Advanced collision detection: Building upon the above, I want the player to know which part of their “body” is colliding.
- Advanced collision correction: When the character detects it is inside an object, it should correct its positioning (based on settings) to the appropriate position.
- Gravity: Whenever not on the ground, a constant gravity force will be applied to the character.
- Wall-climb: The player should optionally be able to climb walls by jumping onto then off of certain walls for added height.
- Wall-Slide: Building upon the wall-climb, the player should fall at a slower rate after jumping onto a wall, and before jumping off.
- Jump queuing: For smoother gameplay, players are able to “queue” jumps by pressing the Jump key a fraction of a second before landing on the ground.
- Jump allowance/Coyote Time: When the player walks/falls off an edge, for a fraction of a second, they are able to jump as if they are still grounded.
If you’re keen to see how I combined all these features into a 391-line script, check out the Mediums I’ll be posting tomorrow and for the next few days. :)