My movement code

Here’s my movement code:

extends KinematicBody

export var speed = 200

const UP = Vector3(0, 1, 0)

func _physics_process(delta):

func move(delta):
	var motion = Vector3(0, 0, 0)
	# Move
	if Input.is_action_pressed("ui_up"):
		motion.z -= 1
	if Input.is_action_pressed("ui_down"):
		motion.z += 1
	if Input.is_action_pressed("ui_left"):
		motion.x -= 1
	if Input.is_action_pressed("ui_right"):
		motion.x += 1
	# Fall
	if not is_on_floor():
		motion.y = -GRAVITY
	# Move
	move_and_slide(motion * speed * delta, UP)

I think we should be applying delta to our movement to have the player movement frame rate independent.

My gravity is the same as my x/z speed. If we start jumping in this game, I would have to do something to make motion.y have a different gravity-driven speed, but if all we’re doing is keeping the character on the ground then this should work.

One thing I notice is that if the player moves into the ball, everything moves as expected, but if the ball rolls into the player, the player lurches forward in a giant step. I assume we will correct that interaction in a future lecture.

Actually, move_and_slide() already adds delta, so if we write delta in we’re actually adding delta squared.

That said, awesome work!

Ah… I forgot about move_and_slide applying delta on it’s own. Thanks!

Privacy & Terms