Movin step by step.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Syl

Greets!

Here’s my script for a smooth movement by arrows:

func _process(delta):
	var input_dir = Vector2()
	if Input.is_key_pressed(KEY_UP):
		input_dir.y += 0.07
		
	if Input.is_key_pressed(KEY_DOWN):
		input_dir.y += 0.07
	
	if Input.is_key_pressed(KEY_LEFT):
		input_dir.x -= 0.07
	
	if Input.is_key_pressed(KEY_RIGHT):
		input_dir.x += 0.07
	
		
	position += (delta * MOVE_SPEED) * input_dir

But i want it to just move a bit in a direction, and then the need of another key_pressed to move further, not a continual move with one key pressed.

Any help?

:bust_in_silhouette: Reply From: Beechside

I would use the following:

func _process(delta):
var new_vector = Vector2()
if Input.is_action_just_pressed('ui_right'):
	new_vector += Vector2.RIGHT
if Input.is_action_just_pressed('ui_left'):
	new_vector += Vector2.LEFT
if Input.is_action_just_pressed('ui_down'):
	new_vector += Vector2.DOWN
if Input.is_action_just_pressed('ui_up'):
	new_vector += Vector2.UP
new_vector = new_vector.normalized()
position += delta * MOVE_SPEED * new_vector * 0.07

The normalized() method makes sure that you don’t move faster in the diagonal directions, since 1 up and 1 right moves you further than just 1 in those directions alone.

Also note that you had the wrong operator in the key up: it should have been –= not +=

HTH

That works exactly as intended, but for my sprite GUI coverin my TileMap: it’s jerkin at every move… Maybe due to the fact that it’s child of my player and camera, but i cannot put it elsewhere… Any idea?

Syl | 2020-01-28 14:11