Godot Version
4.4.stable
Question
if Input.is_action_pressed("down") and Input.is_action_pressed("left") or Input.is_action_pressed("down") and Input.is_action_pressed("right") or Input.is_action_pressed("up") and Input.is_action_pressed("left") or Input.is_action_pressed("up") and Input.is_action_pressed("right"): velocity = velocity.normalized() * speed else: move_toward(velocity.x, 0, deceleration) move_toward(velocity.y, 0, deceleration)
What this codes does is check whether vector needs to be .normalized and normializes it and once you stop pressing the buttons you will then stop. And that all works fine but what i noticed is that it seems that when press forward or backward and left or right my characters speed seems to be slower than just walking normally
This seems unreasonably complicated, you can just use Input.get_vector(…). This is normalized automatically
var direction: Vector2 = Input.get_vector("left", "right", "up", "down")
velocity = direction * speed
How do you calculate the velocity?
1 Like
I also agree with @herrspaten, just wanted to explain the issue.
I think its because you are only normalizing if you are moving in diagonals. But if you move in one direction then the else
statement applies and does the declaration in the move_towards
.
It looks like deceleration is getting applied when you move in cardinal directions.
You can normalize at any time so i would just simplify your logic to just the directions
if Input.is_action_pressed("down")
or Input.is_action_pressed("left")
or Input.is_action_pressed("right")
or Input.is_action_pressed("up") :
velocity = velocity.normalized() * speed
else:
move_toward(velocity.x, 0, deceleration)
2 Likes
Thanks its working properly now