Why is this movement so shaky?

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

Hi guys,
right now I’m starting to learn 3D. My first little project is a simple FPS control scheme. It works, but it is very shaky. Please tell me what I’m doing wrong. Here is my code:

extends RigidBody

const MOUSE_SPEED = 0.01
const WALK_SPEED = 0.2

func _input(event):
	if event is InputEventMouseMotion:
		_turn_head(event.relative * Vector2(-MOUSE_SPEED, MOUSE_SPEED))

func _physics_process(delta):
	if Input.is_action_pressed("walk_forward"):
		translate(Vector3(0, 0, WALK_SPEED))
	if Input.is_action_pressed("walk_backward"):
		translate(Vector3(0, 0, -WALK_SPEED))
	if Input.is_action_pressed("strafe_left"):
		translate(Vector3(WALK_SPEED, 0, 0))
	if Input.is_action_pressed("strafe_right"):
		translate(Vector3(-WALK_SPEED, 0, 0))

func _turn_head(turn_by):
	rotate_y(turn_by.x)
	$Camera.rotate_x(turn_by.y)
	if $Camera.rotation_degrees.x > 90.0:
		$Camera.rotation_degrees.x = 90.0
	elif $Camera.rotation_degrees.x < -90.0:
		$Camera.rotation_degrees.x = -90.0

You are using a RigidBody for character control? I would use a KinematicBody instead.

SIsilicon | 2018-12-23 15:38

I want to have the physics engine to handle gravity etc.

bastilo | 2018-12-23 15:44

It doesn’t take too much code to implement gravity. Besides. It’s much more flexible this way. This is for 2D, but the idea is pretty much the same.

SIsilicon | 2018-12-23 21:27

:bust_in_silhouette: Reply From: anomalocaris

You need to multiply all your velocity vectors by delta, that includes turning so you’ll probably want to move the logic for that into _physics_process or call a movement processing function from _physics_process, passing delta to it. You can find a good example of using delta for movement in the “Your First Game” tutorial in the official documentation. It’s 2D but I highly recommend going through it (and all the stuff that precedes it) even if you only plan on doing 3D. The delta parameter is explained in the official scripting tutorial. It’s mainly used to keep movement smooth and independent of frame rate, so it should fix most of your problems.

There is also an FPS tutorial in the official Godot 3 documentation which has working movement code.You should read the “Introduction to 3D” page first as it explains how the 3D nodes work. Note that I have had several issues with the later parts of the FPS tutorial (they’re a little rough-around-the-edges for something in the official documentation), but the movement code seemed to work fine when I tested it.

:bust_in_silhouette: Reply From: SIsilicon

The reason why it’s shaky is most likely because you’re directly changing the position in a RigidBody. Doing this kinda breaks the physics simulation.