This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Looking at the Godot documentation, I found a code to move towards mouse click:

func _physics_process(delta):
velocity = global_position.direction_to(target) * speed
if global_position.distance_to(target) > 5:
    velocity = move_and_slide(velocity)

I'm stuck on how to get the object to continuously move in that direction. Like if I click somewhere to the right of the player, I want the player to have constant velocity towards where I clicked and not stop. Any suggestions? Thank you!

in Engine by (27 points)

1 Answer

+1 vote
Best answer

You may set the target when mouse click.

Like this following:

var target = Vector2.ZERO

func _physics_process(delta):
    if Input.is_mouse_button_pressed(1): # when click Left mouse button
        target = get_global_mouse_position()
    velocity = global_position.direction_to(target) * speed
    if global_position.distance_to(target) > 5:
        velocity = move_and_slide(velocity)
by (526 points)
selected by
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 Frequently asked questions and 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 [email protected] with your username.