Make sprite move to mouse click position

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

ok I have the following code

func _physics_process(delta):
rotation += get_local_mouse_position().angle() * rotation_speed * delta
velocity = Vector2(speed,0).rotated(rotation)
move_and_slide(velocity)

This works exactly as I wanted. but I wanted to make it that it move so I change the code to

func _input(event):
	if event.is_action_pressed("click"):
		targetpos = get_local_mouse_position()
		cango = true;
func _physics_process(delta):
	if cango:
		rotation += targetpos.angle() * rotation_speed * delta
		velocity = Vector2(speed,0).rotated(rotation)
		move_and_slide(velocity)

But now the sprite just rotates non stop I am not sure what I did wrong. All I did was assign the get_local_mouse_position() to a variable .
Any help is greatly appreciated. Thanks

:bust_in_silhouette: Reply From: Thomas Karcher

get_local_mouse_position() returns the position and angle relative to the current position of the body [1]. In your first example, you updated this value after every movement, so the angle would eventually be reduced to zero after a couple of rotation steps. In the second example, the position and angle is only calculated once in the click event handler, so the same rotation value is applied again and again in the process function.

[1] CanvasItem — Godot Engine (3.1) documentation in English

Thanks for the help , what I wanted to do is click on a place on the map , and ship would move forward while rotating to face to direction of the destination point. From the doc, I am assuming I sholuld use position.angle_to(targetpos) instead ?

lowpolygon | 2019-05-31 11:11

:bust_in_silhouette: Reply From: estebanmolca

Try:

var targetpos=Vector2.ZERO
var cango=false


func _input(event):
  if(event.is_action_pressed("click")):
	cango=true
  if(event.is_action_released("click")):
	cango=false

func _physics_process(delta):
  if(cango):
	targetpos=get_local_mouse_position()
  rotation=targetpos.angle() * rotation_speed * delta
  velocity=Vector2(speed,0).rotated(rotation)
  move_and_slide(velocity)

I do not understand the behavior you expect, but input does not work as a loop, therefore the variable targetpos is not updated.

The kind of behavior I wanted to do is I would click on a point on the map and ship would move forward while rotating to face to destination point. I think I got the movement behavior the way I wanted, I just need to make the click on the area part right
But thanks for the help anyway

lowpolygon | 2019-05-31 11:12

ok, if I did not understand wrong, you want a movement like LOL or DOTA2, I could do it but I’m seeing how to implement the speed of rotation (turn rate). Here I leave it, I’m not sure it’s what you’re looking for, but I’m learning from this.
(Sorry my english, use google translate for spanish)

extends Node2D
onready var sp=$Sprite
var target_pos=Vector2.ZERO
var mouse_pos=Vector2.ZERO
var velocity=Vector2(0.05,0.05)
var move_on=false

func _input(event):
	if(event.is_action_pressed("lmb")):
		move_on=true
		mouse_pos=get_local_mouse_position()

func _physics_process(delta):
	target_pos=mouse_pos
	if(move_on):
		sp.position+=(target_pos-sp.position)*velocity
		sp.rotation=sp.position.angle_to_point(target_pos)
		

estebanmolca | 2019-05-31 14:59