Getting object to follow mouse

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

Hi, I was wondering about how i can get an object to follow my mouse like haveing it connected to it. For my game currently, I am able to get the balloon (object) to be dragged when clicked. I would like it to connect to the mouse so when I click on my mouse the object appears where the mouse it and so that u can drag and drop it anywhere.

:bust_in_silhouette: Reply From: larrxi

In balloon:

var is_following_mouse = false

In balloon _update():

If is_following_mouse:
    Set balloon position to mouse position

Also make when balloon is clicked set is_following_mouse to true.

If you want multiple balloons instead save the balloon in variable.

If you have different nodes you want object be in you can do this https://youtu.be/usWuBrrh5lQ

hey, larrxl, if you put 4 spaces before every line of code, it will be a lost easer to read. You’ll get,

is_following_mouse

instead of

is following mouse

it really makes the code much easier to follow. anyway, not trying to be a critic, its just a suggestion.

Millard | 2020-10-18 01:11

Thanks. Looks much better now

larrxi | 2020-10-18 01:24

For the last line Set balloon position to mouse position, is there a proper code for this because I am unsure of how the write out the code properly

dang_ | 2020-10-18 12:16

in the _physics_process(delta): (godot version of the Update() function)

you would probably write something like:

var mouse_position = get_global_mouse_position()

and in the last line you would write:

balloon.position = mouse_position

hope this helps. :slight_smile:

Millard | 2020-10-18 17:40

Thanks. Unfortunately, the balloon.position comes up as an error, how fo i declare it properly. should it be like
var balloon.position equals something???

dang_ | 2020-10-20 05:37

Which object is your script attached to?

If script is in balloon try

position = mouse_position

Also global might work better:

global_position = mouse_position

larrxi | 2020-10-20 05:49

it says that the member already exist in a parent class. Would it be easier if I just send the whole code I have. Also the code is in the balloon

dang_ | 2020-10-20 05:51

Yes send the code :slight_smile:

larrxi | 2020-10-20 05:57

extends KinematicBody2D

var dragging = false
var is_following_mouse = false
var mouse_position = get_global_mouse_position()
var global_position = mouse_position

signal dragsignal;

func _on_Balloon_body_entered(body):
if body.get_name() == “Fire”:
body.queue_free()

func _on_Balloon_ready():
connect(“dragsignal”, self, “_set_drag_pc”)

func _process(delta):
if dragging:
var mousepos = get_viewport().get_mouse_position()
self.position = Vector2(mousepos.x, mousepos.y)

func _set_drag_pc():
dragging=!dragging

func _on_Balloon_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
emit_signal(“dragsignal”)
elif event.button_index == BUTTON_LEFT and !event.pressed:
emit_signal(“dragsignal”)
elif event is InputEventScreenTouch:
if event.pressed and event.get_index() == 0:
self.position = event.get_position()

func balloon_updated():
if is_following_mouse:
balloon.position = mouse_position

dang_ | 2020-10-20 06:02

There is a code tag you can use to make it show better here

I think you have to make _ready

func _ready():
    onBalloon_ready() # maybe misspelled

Now in _process add as a last line inside the if:

onBalloonready()

And you will see if it activates dragged and if it prints the mouse position.

larrxi | 2020-10-20 06:20

it says it can’t assign to an expression. This is whats I’ve changed, is there any way you could fix the code for me because I really am confused

extends KinematicBody2D

var dragging = false
var is_following_mouse = false
var mouse_position = get_global_mouse_position()
var global_position = mouse_position

signal dragsignal;

func _on_Balloon_body_entered(body):
if body.get_name() == “Fire”:
body.queue_free()

func ready():
onBalloon_ready()

func _on_Balloon_ready():
connect(“dragsignal”, self, “_set_drag_pc”)

func _process(delta):
if dragging:
var mousepos = get_viewport().get_mouse_position()
self.position = Vector2(mousepos.x, mousepos.y)

func _set_drag_pc():
dragging=!dragging

func _on_Balloon_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
emit_signal(“dragsignal”)
elif event.button_index == BUTTON_LEFT and !event.pressed:
emit_signal(“dragsignal”)
elif event is InputEventScreenTouch:
if event.pressed and event.get_index() == 0:
self.position = event.get_position()

func balloon_updated():
if is_following_mouse:
onBalloon_ready() = mouse_position

dang_ | 2020-10-20 06:29

Probably your last line make the error. I think you can comment it out

#func balloonupdated():
#    if isfollowingmouse:
#    onBalloonready() = mouse_position

Please select your code and click the {} picture.
Also if you have error paste in the error also

larrxi | 2020-10-20 06:40

There isnt any error but the object wont follow the mouse. I want it to follow without having to click on it, just follow no matter what. If your wondering why its because I want it to be invisible unless u click and then it appears. then u drop it in the spot u want.

dang_ | 2020-10-20 06:51

You mean if click once then you want it to follow the mouse. Then second click place it?

Is this correct:
So in the world is first nothing.
Then click anywhere and a baloon appears and follow mouse.
Second click places ballon on click position.

Now nothing follows mouse.

Third click makes another balloon that follows mouse.
Fourth click places balloon on click position.

Now nothing follows mouse.

Or just one balloon??

larrxi | 2020-10-20 07:00

its supposed to be continuous until u run out of ammo but I have no idea how to do that so please if u know how send anything u know

dang_ | 2020-10-20 07:05

In that case you better move all the clicking and mouse things out from balloon.

How to do this:
Print every line and make sure they work or check in godot remote.
Next comment you ask you can say what works and what step is not working.

Step 1:
You need to get the mouseclick from the container or world.
Set all the mousefilters to pass on every control or panel.
Print mouse_click and make sure it change values when you move around mouse.

In this container Set a variable

Var gabbed_balloon =false

Step 1.5:
Connect step 1 clicks with handle_mouse_click.

Step 2:

func Handle_mouse_click(pos):
    If grabbed_balloon=false:
        #make a balloon. Idk maybe make Ballon.tscn and var balloon= Ballon.new()
        #assign that balloon to grabbed_balloon
        #remember add_child(balloon)
    Else:
        Grabbed_balloon=false

Step 3:
Here you going to update the eventual balloon that is being grabbed.

Func _physics_process(_delta):
    If grabbed_balloon not false:
      Grabbed_balloon.global_position = get_global_mouse_pos()

larrxi | 2020-10-20 07:44

wait where do i move all the clicky stuff and mouse stuff to

dang_ | 2020-10-20 07:58

Well is the world as big as the windows? Then it would be one of the things covering the whole world. Click 2D view and when you select your nodes/object to te left and it will outline a line of the border.

If you cant figure it out you have to describe your tree from root node down to balloon node or node where balloons gonna be.

larrxi | 2020-10-20 08:11

do u know how i could make a simple counter??

dang_ | 2020-10-20 08:23

Yes

var amount = 0

amount += 1
amount += 1
amount += 1
amount += 1

larrxi | 2020-10-20 08:40

do you have a full code that I can just plug in for the balloon because I’m really unsure still

dang_ | 2020-10-20 09:17

Send the godot project

larrxi | 2020-10-20 09:38

Im not sure how

dang_ | 2020-10-20 09:38

One way is to make a github.com account and make repository of your godot project and upload to there

larrxi | 2020-10-20 11:50

:bust_in_silhouette: Reply From: larrxi

I made a project that make you 5 balloons.

Code:
https://github.com/Larrxi/godot-balloon-follow-mouse

Run it in browser on itch.io:
https://larrxi.itch.io/godot-balloon-follow-mouse