0 votes

Hi everyone, I have a problem with the code regarding "making the object move towards the mouse pointer". I followed the tutorial on godot official documentation but it didn't work. So I decided to do it without following the documentation but the problem is that when I click to make the object go to a given position, the object goes in that direction but it doesn't stop. Here is the code:

var dir = Vector2()
var speed = 200

func input(event):
if event is InputEventMouseButton:
dir = position.direction

func physicsprocess(delta):
moveandslide(dir * speed)

Godot version 3.2.3
in Engine by (54 points)

2 Answers

0 votes

First off, I'm pretty sure that the official tutorial should work. Did you add "click" to the InputMap in the project settings?

Second, the official tutorial has a check that makes it stop moving once it gets close enough to the target. You don't have a target, only a direction, so there is no way to stop the object. I think you should try going through the official tutorial again.

by (8,150 points)

In the official tutorial I saw that the object stops (the thing that doesn't work for me) when it stops moving towards a point indicated by the mouse pointer! However it seemed strange to me too that an official tutorial code doesn't work.

0 votes

You can do something like this:

func _input(event):
    if event is InputEventMouseButton and event.pressed:
        target = event.position

func _physics_process(delta):
    if target != Vector2.ZERO and target != position:
        position=position.move_toward(target, velocity.length())
by (809 points)
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.