+1 vote

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

Godot version Godot 4 beta 4
in Engine by (16 points)

1 Answer

+6 votes
Best answer

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 https://docs.godotengine.org/en/latest/classes/class_characterbody3d.html and the default built-in CharacterBody3D script for details.

by (21,967 points)
selected by
move_and_slide just for an axis
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.