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
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.
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 ?
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)