Why is this movement so shaky?

Attention Topic was automatically imported from the old Question2Answer platform.

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:

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))

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

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.
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.