In your code
var velocity = Vector2()
export (int) var speed = 400
func get_input():
velocity = Vector2()
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
velocity = velocity.normalized() * speed
func _physics_process(delta):
get_input()
velocity = move_and_slide(velocity)
if you don't use normalized()
nothing will happen because your player only moves left
or right
but if your player moved up
and you pressed up
and left
at the same time , your player will move faster. your player would move 1.414
times faster in top -left direction.
It is because velocity
is(1,1)
and its magnitude is sqrt( 1*1 +1*1) = 1.414
While if you do normalized() velocity
is (0.707,0.707)
and its magnitude is sqrt( 0.707*0.707 + 0.707*0.707) = 1
we need to keep its magnitude to 1 so we use normalized()