How to create your own physics-based character controller using the new Unity input system.

In this article I will go over how to create a simple physics based character controller which will be used for a 2.5d platformer.

I have created a simple scene for what could be a 2.5D platformer. I have added some primitive cubes as platforms and a 3d capsule for our player.

First step is to add a character controller onto your player, ensuring the collider matches the dimensions of your game object.

Next lets create our Input controls. In the project view right click or press the plus button and select “Input Actions”. This will create a new input actions asset for you. Go ahead and name it what you want, I called mine “Controls”.

Open your input actions asset and create a new action map (mine will be called Player). From here we can create a movement action (just rename the default). Change the action type to “Value” and the control type to “Vector 2”. Next right click on the movement action and select add a “2D Vector Composite”. Delete the up and down actions. Add your “ a “ and “d’ buttons to the path for the left and right actions. Don’t forget to save the asset.

Next create a player script. On the script get a reference to your character controller.

We are going to use the input system name space (“using UnityEngine.InputSystem;”) to give us access to the callbacks for our actions. We will assign these to an input manager later.

We will set up our method to receive our movement values. Our input action will pass through a value of -1, 0, or 1 depending on the direction the player is moving. We access the context of our input action call back, and read the vector 2 x value. I have created a variable called “_horizontalInput” which we will assign our “Movement” action value to.

We also want our player to be able to jump so I’ve added another input action “Jump”

The logic will be that the player can’t change directions or move their character while they are in the air after jumping. We will need to assign a value for jump height and a gravity value.

Next step is to create a method for movement. In this method we create a new vector 3 (“_direction” ) taking in the _horizontal input from earlier, and a speed value for the player speed.

We can create a new vector 3 for a velocity which is going to take in our horizontal input and speed. This value will be passed into the move function of our character controller.

We next need to apply our gravity. We will only let the player move if they are grounded. So we move our code into an “if function” and check the inbuilt “isGrounded” method on the character controller. If the controller is grounded we will allow the direction to be updated otherwise we will just continue to apply gravity to our velocity on the y axis.

Lets set up the jumping functionality next. After the jump button is pressed we will check to see if the controller is grounded and apply our jump height to a new variable (“_yVelocity”).

The purpose for this new variable is to provide a cached version of our velocity.y to prevent it from being overridden when our player changes direction in the movement script. I have also added a variable to check for jumping as we will use this in my next article to enable a double jump.

Back in our movement function we will do a grounded check and check to see if the _yVelocity is less than or equal to zero to turn our jumping bool to false, this prevenst the jumping bool from immediately being turned false after we jump. We can also apply our gravity to our _yVelocity variable and then pass this value over to our velocity.y. The last step in our movement function will be to applied the velocity to our character controller movement function.

The next step is to create an input manager. Right click in your hierarchy and add a new empty game object called “InputManager”. Add a player input component to this object and assign the controls input action to the actions field. Change the behavior to “Invoke Unity Events”. Expand the events sections and under our player there should be the movement and jumping actions we added to our input actions earlier. Click the plus button and then assign the player script to both. Under the player script assign the appropriate function for each of the actions.

Now all we need to do is test our functionality.

That’s all for now.

--

--

--

Unity developer with a love of learning all things programming.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ARTH Task 13

Kubernetes Local Development: The Correct Way

Query SQL Server using Powershell to output a CSV

Design Patterns 02 — Prototype Design Pattern

The 2021 Gartner Magic Quadrant for Public Cloud IT Transformation Services

Azure Managed Identity to access Key Vaults

Modernize IBM i Solutions With Powerful Enterprise Portal for the Web

Top 10 things you need to know about Django 4.0

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luke Duckett

Luke Duckett

Unity developer with a love of learning all things programming.

More from Medium

How to Add Custom Post Processing Files to Individual Cameras in Cinemachine

Working with Screen Space Reflections in Unity’s HDRP

Adding Post Processing to our Prototype

How to Install the Universal Render Pipeline