3D Character/RigidBody Movement

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By steffenkre
:warning: Old Version Published before Godot 3 was released.

Whats the ‘best’ way to move a character in 3D space. I have a Player Noder (Rigidbody, Mode: Character) and want it to make move forward(depending on its rotation), rotating and jumping.
My first naive attemp was:

extends RigidBody
export var rotspeet = 1.0
export var movespeed = 1.0

func _ready():
	# Called every time the node is added to the scene.
	# Initialization here
	set_process(true)
	
func _process(delta):
	if Input.is_action_pressed("ui_right"):
		self.rotate_y(rotspeet*delta)
	if Input.is_action_pressed("ui_left"):
		self.rotate_y(rotspeet*delta*-1.0)
	if Input.is_action_pressed("ui_up"):
		self.translate(Vector3(movespeed*delta, 0.0, 0.0 ) )

but the physic simulation stops while rotating/translating. So, whats the best way to move arround in 3D?

:bust_in_silhouette: Reply From: eons

1st, do not use _process for physics stuff, use _fixed_process instead.

Rigidbodies are not supposed to be moved that way, the movement should be managed by the engine.
For regular physics, just use impulses and forces on fixed process and let the engine calculate the motion (be careful not to add a lot of forces per frame because it will accelerate a lot), use damp and friction to stop the body, you can set velocities on this part but try to not do it every frame.

Other option, if you have custom physics, is to use a custom integrator to modify the body state (in _integrate_forces), there you can alter the velocities that the engine will use on every step.

Thnaks a lot for your answer. But how can i move the Rigidbodies along its forward facing direction?

steffenkre | 2017-11-19 11:46

If the relative forward velocity is Vector3(0,0,10), rotate it (the velocity) using the transform basis, something like:

get_transform().basis.xform(velocity)

The result can be applied as absolute velocity.

eons | 2017-11-19 13:22

what method did you recommend for adding vilocity?

steffenkre | 2017-11-19 18:10

if you can manage with something simple as apply_impulse in fixed process, do that; if you need some “less natural” changes, use the custom integrator and add/modify the velocities on the state.

Check the platformer 3d demo for an example of a full character movement script using a rigid body.


If you can avoid using a rigid body, like using a kinematic body, go that way because you will have more control of everything.
The only issue with KinematicBody on Godot 2.x is that it only uses layers for collisions and can’t use masks, reducing the collision layer capabilities.

eons | 2017-11-19 19:33

:bust_in_silhouette: Reply From: Xero

This might help
A video about 3d character movement