0 votes

ok I have the following code

func physicsprocess(delta):
rotation += getlocalmouseposition().angle() * rotationspeed * delta
velocity = Vector2(speed,0).rotated(rotation)
moveandslide(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 getlocalmouse_position() to a variable .
Any help is greatly appreciated. Thanks

in Engine by (173 points)

2 Answers

0 votes

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] https://docs.godotengine.org/en/3.1/classes/class_canvasitem.html#class-canvasitem-method-get-local-mouse-position

by (1,742 points)

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 ?

0 votes

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.

by (2,098 points)

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

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