0 votes

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)
setprocessinput(true) func process(delta):
var posicao
personagem = getnode("mega").getpos() 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)

in Engine by (19 points)

1 Answer

0 votes

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))
by (1,328 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.