Problems with diagonal movement key press timing

Godot Version



Hi, I’m kinda new to Godot and game development and I’m having problems implementing player movement the way I wanted it to look like.

If I’m pressing two keys to move diagonally and then don’t release them at the exact same time, the engine thinks only one key was pressed at the end. It wouldn’t be such a problem if it weren’t for my idea to rotate the character sprite according to velocity and then also have the character move a little bit after the input is stopped.
It doesn’t feel good to play at all because of that and it’s really annoying to have to release the keys perfectly on the same frame…

Is there any way to deal with that? Make it so even if the timing is a little off, the sprite won’t get rotated and keep moving in the wrong direction?

Here’s my code for context:

extends CharacterBody2D

const SPEED = 120.0
const FRICTION = 200.0

func _physics_process(delta):
	var sprite = $"."

	var direction = Input.get_vector("move left", "move right", "move up", "move down")
	if direction:
		velocity = direction * SPEED
		velocity.x = move_toward(velocity.x, 0, FRICTION * delta)
		velocity.y = move_toward(velocity.y, 0, FRICTION * delta)
	if velocity > Vector2(1, 1):
	var trail = $trail

	if velocity.length() > 0:
		var angle = velocity.angle() + deg_to_rad(90.0)
		sprite.rotation = angle
	if velocity.x != 0 || velocity.y != 0:
		trail.emitting = true
		trail.emitting = false


Thanks in advance for any help!

You will have to induce a delay of some kind. The easiest would be use move_toward for setting the speed and well as reducing it to zero

if direction:
    velocity = velocity.move_toward(direction * SPEED, ACCELERATION * delta)

var sprite = $"."

This line is a slow equivalent to self, and also not a sprite child of your character. It’s being used to set rotation so the following lines are the same and you should prefer the latter

sprite.rotation = angle
rotation = angle

It works!
Thank you so much!