• 3D
  • How move an object between two vectors?

I am trying to move a player in a direction using the keys, i need control the object between two vectors, i am testing this code:

velocity = Vector3(0.1,0.5,0)
if Input.is_action_pressed('ui_right'):
	velocity.x += 1
	global_translate(velocity)



if Input.is_action_pressed('ui_left'):
	velocity.x -= 1
	global_translate(velocity)

The problem is that the player is changing of position, i want move only between two points.

Maybe use position.move_toward(target_position, velocity)

a month later

The translate , global_translate function is used to move from the current position. The function to call is set_translation, since you want to move it to the specified location.

Example)

func _input(event):

    var velocity = Vector3(0.1, 0.5, 0)

    if Input.is_action_pressed("ui_right"):
        velocity.x += 1
        set_translation(velocity)

    if Input.is_action_pressed("ui_left"):
        velocity.x -= 1
        set_translation(velocity)