3D First person Movement Lags Like Word Processor

Godot Version

3.5.3 stable win64

Question

I am trying to make a 3D game with a first person character controller using Godot 3.5.3 and gdscript. Most of everything works except when I press and hold the move forward key on my keyboard, or any other movement key, while running the game, I move forward just a little, it waits a second, then moves forward and stops in quick succession with faster pauses between moving forward. Almost like how in a word processor, if you press and hold a key, it types it once, waits a second, then types it a bunch of times. Any help would be greatly appreciated. Thank You!

# Handle player movement
	var velocity = Vector3()
	if Input.is_action_pressed("move_forward"):
		velocity.z -= speed
	if Input.is_action_pressed("move_backward"):
		velocity.z += speed
	if Input.is_action_pressed("move_left"):
		velocity.x -= speed
	if Input.is_action_pressed("move_right"):
		velocity.x += speed
	velocity = velocity.rotated(Vector3.UP, rotation.y)
	move_and_slide(velocity)

This code looks OK, I’d say the issue is somewhere else. Maybe post more of the player movement code?

2 Likes

Thank you struhy_xd here is all of it so far:

extends KinematicBody

export var speed = 14

func _ready():
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _input(event):
	# Looking Around With The Mouse
	var camera_horizontal = 0
	var camera_vertical = 0
	if event is InputEventMouseMotion:
		camera_horizontal = event.relative.x / 500
		camera_vertical = event.relative.y / 500
		camera_vertical = clamp(get_node("Camera").rotation.x - camera_vertical, deg2rad(-90), deg2rad(90))
		rotation.y = rotation.y - camera_horizontal
		get_node("Camera").rotation.x = camera_vertical
	
	# Handle player movement
	var velocity = Vector3()
	if Input.is_action_pressed("move_forward"):
		velocity.z -= speed
	if Input.is_action_pressed("move_backward"):
		velocity.z += speed
	if Input.is_action_pressed("move_left"):
		velocity.x -= speed
	if Input.is_action_pressed("move_right"):
		velocity.x += speed
	velocity = velocity.rotated(Vector3.UP, rotation.y)
	move_and_slide(velocity)

Oh I think I know what’s going on. As you said, similar event to the word processor hold probably starts firing after you hold down a button for a while.

You don’t want the movement logic in _input. Put it into _physics_process. (Conceptually you want to be always moving, not just when an input happens. The movement code executes every frame, but when no action is pressed, the player slows down or something.)

Looking around only when mouse moves is completely fine. Also probably do a type check on the event to separate mouse movements from clicks and key presses.

2 Likes

Thank you very much! Your help will be much appreciated in future projects of mine!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.