Help me with a point-and-click movement

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DanniloSN
:warning: Old Version Published before Godot 3 was released.

I’m trying to do a point-and-click, I’m using set_pos (), but when the character reaches the destination, it’s quivering without stopping. Can you help me fix this? Or replace with the move?

extends Node2D

const VELOCIDADE = 100.0

onready var personagem = get_node("mega")

var direcao
var movimento
var destino = Vector2()

func _ready():
	set_process(true)
	set_process_input(true)

func _process(delta):
	var posicao_personagem = get_node("mega").get_pos()

```
direcao = (destino - posicao_personagem).normalized()
movimento = delta * VELOCIDADE

if posicao_personagem + direcao > destino:
	personagem.set_pos(posicao_personagem + direcao * movimento)

get_node("Label").set_text(str(personagem.get_pos()))
```

func _input(event):
	if event.type == InputEvent.MOUSE_BUTTON and event.is_pressed():
		destino = event.pos
		print(destino)
:bust_in_silhouette: Reply From: YeOldeDM

What you want to do, is detect when the player is “close” (maybe within one pixel) of its destination, and have it snap to that position.

var pos = player.get_pos()
var destination = event.pos

var D = pos.distance_to(destination)
if D < 1.0:  pos = destination

if pos != player.get_pos(): player.set_pos(lerp(pos,destination,delta))

Also, add some flags to start and stop movement (start on mouse/action press, stop on close to destination).

eons | 2016-12-20 21:43