My movement code

Here’s my character movement solution:


func _process(delta):
	move()
	face_foward()


func move():
	motion = Vector3(0, 0, 0)
	if Input.is_action_pressed("forward"):
		motion.z -= 1
	if Input.is_action_pressed("back"):
		motion.z += 1
	if Input.is_action_pressed("left"):
		motion.x -= 1
	if Input.is_action_pressed("right"):
		motion.x += 1
	motion = motion.normalized()
	if motion.length() > 0:
		# Rotation to make the unanimated character face the direction of movement
		facing_direction = atan2(motion.z, -motion.x)
		# Rotation to make animated character face the direction of movement
		var facing_facing_direction2 = atan2(-motion.x, -motion.z)
	move_and_slide(motion * SPEED, UP)


func face_foward():
	$Armature.rotation.y = facing_direction

To accommodate moving diagonally, I’m normalizing the motion vector (so pressing forward and left doesn’t make us move twice as fast), and calling atan2 to calculate the facing direction based on the x and z movement.

The above code causes the unanimated player to face the correct direction. We ultimately need to use a different angle to compensate for the rotated animations. The second atan2 call calculates that angle.

Privacy & Terms