How move an object in a particular direction

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

I am trying to move an object with the keys in a specific direction using a vector:

velocity = Vector3(0.1,0.5,0)#the direction

if Input.is_action_pressed('ui_down'):
	velocity.y += 1
	global_translate(velocity)

if Input.is_action_pressed('ui_up'):

	velocity.y -= 1
	global_translate(velocity)

I want control the object in this direction, but the position of the object is changing every time that press the keys.

:bust_in_silhouette: Reply From: hazlin

Hey loml666,

I think what you are trying to do, is have an object travel on a line described by a vector, and you want the controls to change whether it is moving positively or negatively along the vector.

Now I may have miss understood the question, but if I didn’t here is a 2d example of what I described.

extends Sprite

var direction : int = 0
var axis = Vector2(0.1,0.5)
func _process(delta):
	
	if Input.is_action_pressed("go_north"):
		direction = -1

	if Input.is_action_pressed("go_south"):
		direction = 1
		
	if Input.is_action_pressed("go_stop"):
		direction = 0

	var velocity = axis * direction
	global_translate(velocity)