# Utilizing the CharacterBody 2D and 3D nodes in Godot 4

Working with the node formerly known as KinematicBody

## The node formerly known as KinematicBody

When making 3D games in Godot 3 it was very common to base the player character on the `KinematicBody` node as they are a special type of Physics Body ment to be user controlled.  The equivalent for 2D games was the `KinematicBody2D`.

One goal in Godot 4's development was to simplify the node tree and make the naming more consistent.  To that effort, `KinematicBody` was renamed to `CharacterBody3D` and `KinematicBody2D` was renamed to `CharacterBody2D`.  This was a common change for nodes with 2D/3D counterparts.  No longer is the lack of a suffix equated to *implied 3D*, the 3D is now explicitly appended.  The documentation lists some of these [renamings](https://docs.godotengine.org/en/latest/tutorials/migrating/upgrading_to_godot_4.html?ref=exploregame.dev#automatically-renamed-nodes-and-resources).

![KinematicBody and KinematicBody2D on the left with arrow pointing right to their new names; CharacterBody3D and CharacterBody2D](https://cdn.hashnode.com/res/hashnode/image/upload/v1707596821956/9f473eb8-1bdd-4c1e-afad-806f8f736ced.png align="left")

This class consistency renaming is also apparent if you compare the the class hierarchies of `KinematicBody` and `CharacterBody3D`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707596822884/704efd9f-3638-4a27-bfd4-b1b8a3b984a4.png align="left")

## New functionality in CharacterBody

For the remainder of this article we'll focus on `CharacterBody3D` specifically but much/most of this is relevant to `CharacterBody2D` also.

### Few more methods

If we compare the methods exposed for `KinematicBody` vs `CharacterBody3D` we see some expansion. For instance in addition to `is_on_floor`, we have `is_on_floor_only`.  The former returns true if the body collided **only** with the floor on the last call of `move_and_slide`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707596824087/abc6a3b2-8765-4fb1-84d2-93265c4ba083.png align="left")

### Changes with `move_and_...` methods

In Godot 3 methods such as `move_and_slide` had gotten a bit unruly with regards to their method signatures.

\# KinematicBody

move\_and\_slide ( Vector3 linear\_velocity, Vector3 up\_direction=Vector3( 0, 0, 0 ), bool stop\_on\_slope=false, int max\_slides=4, float floor\_max\_angle=0.785398, bool infinite\_inertia=true )

With this *function centric* approach, as more features were added to the engine for which move and slide would need to be informed of, we would need to add yet another param to the method with a default value to protect backwards compatibility.  This doesn't scale well and makes the method overly complex.

In Godot 4, in a more *object oriented* design, these parameters have been moved out to first class properties of the `CharacterBody3D` class.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707596825280/d5fb3605-b390-47d4-9bfc-78701708473f.png align="left")

So now in `CharacterBody3D` we manipulate these properties on the object prior to calling `move_and_slide` which now has zero parameters and simply returns `true` if the body collided, otherwise, returns `false`.

We can see this in play in the new Godot provided `CharacterBody3D`  script template for basic movement.

extends CharacterBody3D

const SPEED = 5.0 const JUMP\_VELOCITY = 4.5

\# Get the gravity from the project settings to be synced with RigidBody nodes. var gravity: float = ProjectSettings.get\_setting("physics/3d/default\_gravity")

func \_physics\_process(delta: float) -&gt; void: # Add the gravity. if not is\_on\_floor(): velocity.y -= gravity \* delta

\# Handle Jump. if Input.is\_action\_just\_pressed("ui\_accept") and is\_on\_floor(): velocity.y = JUMP\_VELOCITY

\# Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions. var input\_dir := Input.get\_vector("ui\_left", "ui\_right", "ui\_up", "ui\_down") var direction := (transform.basis \* Vector3(input\_dir.x, 0, input\_dir.y)).normalized() if direction: velocity.x = direction.x \* SPEED velocity.z = direction.z \* SPEED else: velocity.x = move\_toward(velocity.x, 0, SPEED) velocity.z = move\_toward(velocity.z, 0, SPEED)

\# Note, now zero parameters for move\_and\_slide move\_and\_slide()

## Wrap up

As you can see core classes such as the formerly named Kinematic Bodies have been unified and simplified in Godot 4 in the form of the Character Body classes.  This is only one small piece of the improvements of Godot 4 and you can find a few more details [in the docs here](https://docs.godotengine.org/en/latest/tutorials/migrating/upgrading_to_godot_4.html?ref=exploregame.dev).

I hope you found this useful and I encourage you to subscribe to this newsletter and the [ExploreGameDev YouTube channel](https://www.youtube.com/@exploregamedev?ref=exploregame.dev) to get notified of new posts here and tutorial videos on YouTube.
