How does move_and_slide() work in Godot 4 beta 4

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Westleyaaj

I was converting one of my projects and I can’t figure out how to use move_and_slide()
on a CharacterBody3D

:bust_in_silhouette: Reply From: kidscancode

It works in the same way it does in 3.x, but the function’s parameters are now properties of the body.

velocity is now a node property, along with up_direction, etc. And move_and_slide() now takes no arguments.

So, the following code for basic movement in 3.x:

extends KinematicBody

var gravity = 10.0
var speed = 3.0
var velocity = Vector3.ZERO

func _physics_process(delta):
    velocity.y += gravity * delta
    velocity.x = Input.get_axis("ui_up", "ui_down") * transform.basis.z * speed
    velocity = move_and_slide(velocity, Vector3.UP)

Would become

extends CharacterBody3D

var gravity = 10.0
var speed = 3.0

func _physics_process(delta):
    velocity.y += gravity * delta
    velocity.x = Input.get_axis("ui_up", "ui_down") * transform.basis.z * speed
    move_and_slide()

See CharacterBody3D — Godot Engine (latest) documentation in English and the default built-in CharacterBody3D script for details.