Creating a Physics Based Character Controller from Scratch in Unity

Physics Based Character Controller part 1

In this article we will create a Character Controller from scratch without using a rigidbody. Normally the rigidbody component will handle the physics part, but we choose to write our own code to make our player move how we want.

In the Editor

Add CharacterController component to Player

In the Player Script

Create handle to CharacterController component and assign it in Start().
In Update() we get the horizontal input and set it as our direction. We only need horizontal input since our game will be a platformer. Velocity is direction with speed. Speed for our player is set at the top of our script.
At the last part we assign the velocity to move with to our character controller corrected by time it took since last frame.

//handle
private CharacterController _controller;
//config
private float _speed = 3f;
void Start()
{
_controller = GetComponent<CharacterController>();
if (_controller == null) { Debug.LogError(“player.charactercontroller is null”); }
}
void Update()
{
//get horizontal input = direction
Vector3 direction = new Vector3(Input.GetAxis(“Horizontal”), 0);
//velocity = direction with speed
Vector3 velocity = direction * _speed;
//move player with velocity
_controller.Move(velocity * Time.deltaTime);
}

--

--

--

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

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

Recommended from Medium

Step by step guide to add Jenkins slave nodes

Jenkins Slave

Which Framework to Choose: Comparing Django vs Ruby on Rails

How to install flutter in your Window system.

AWS Solution Architect Associate exam & tips 2020 SAA2

Building a Flutter Web Container

Testing HTTP Callouts using SFCraft MockServer: Part 1

State Design Pattern

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
Tristan Engel

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

More from Medium

Collectables for the Player

Automate Player Input with Unity Test Runner and NSubstitute

Dash Move In Unity

Unity Physics 3D