Beginner: Can't turn left while "sprinting"

Godot Version

4.2.2.stable

Question

Hi, absolute beginner, started yesterday. So let me know how I can improve my post method.
I followed the step by step on scripting player input on Godot Docs, very useful.

The intended script reads like this:

extends Sprite2D

var speed = 400
var angular_speed = PI


func _process(delta):
	var direction = 0
	if Input.is_action_pressed("ui_left"):
		direction = -1
	if Input.is_action_pressed("ui_right"):
		direction = 1

	rotation += angular_speed * direction * delta

	var velocity = Vector2.ZERO
	if Input.is_action_pressed("ui_up"):
		velocity = Vector2.UP.rotated(rotation) * speed

	position += velocity * delta

I decided to play around a bit and add a backwards move, by turning velocity negative and a “sprint” that doubles the position increase when space is pressed.

extends Sprite2D

var speed = 400
var angular_speed = PI

func _process(delta):
	var sprint = 1
	if Input.is_action_pressed("ui_select"):
		sprint *= 2
	var direction = 0
	if Input.is_action_pressed("ui_left"):
		direction = -1
	if Input.is_action_pressed("ui_right"):
		direction = 1
	rotation += angular_speed * direction * delta
	var velocity = Vector2.ZERO
	if Input.is_action_pressed("ui_up"):
		velocity = Vector2.UP.rotated(rotation) * speed
	if Input.is_action_pressed("ui_down"):
		velocity = Vector2.UP.rotated(rotation) * speed * -1
	position += velocity * sprint * delta

That works all fine except that while sprinting I can’t turn left.
I can still turn right, so my guess is that the negative direction value is messing with it, but I can’t figure out why.
var sprint and var direction do not interact as far as I can see.

Thanks for the help, I’m having a blast figuring out stuff.

It’s possible that your keyboard cannot detect space and left arrow at the same time due to a hardware limitation. Try replacing the space key with shift and see if that fixes the issue.

1 Like

That was it. I didn’t know that could happen. Thank you.

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