Add Gravity to Player

Physics Based Character Controller part 2

In this article we will add gravity to our player controller.

When player is NOT touching the ground we apply gravity. This is a float number we set at the top of our script. We can simply subtract gravity from the y value of our velocity.

void Update()
{
Vector3 direction = new Vector3(Input.GetAxis(“Horizontal”), 0);
Vector3 velocity = direction * _speed;
if (_controller.isGrounded == true)
{
//do nothing
}
else
{
velocity.y -= _gravity;
}

_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

Validate Email Workflows with a Serverless Inbox API

Learning to create a Node.js server as a Mobile Developer

Tokyo Ghoul — TryHackMe

Testing I/O Performance with Sysbench

A Day of Backend

Top Tutorials To Learn Mongo DB

Using make and autoconf with Terraform — Part 1

Time & Space Complexity

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

Live, die and respawn

Static Libraries in C

Bedroom Pop rising vol. 1

Useful tweaks for Windows: get more performance and protect your privacy