Why it so hard? :D Looking weapon at mouse

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JowannoSebastiano
:warning: Old Version Published before Godot 3 was released.

Guys, hello.
There is a trouble with making my weapon looking at the mouse.

The code I use is:

extends Sprite
func _process(delta):
    var mpos = get_viewport().get_mouse_pos()
    look_at(mpos)
func _ready():
    set_process(true)


P.S. I saw an example “LookAtMouse”, there is no need to talk about it.

can you give more information? what is happening with the gun? its not moving at all? its moving weird? looking at look_at Method in godot, it takes two Vector3 arguments.

tiernich | 2016-03-30 15:41

It moves weird, right.
So how can I solve it?

JowannoSebastiano | 2016-03-30 19:25

Have you set the rotation point of sprite?

puppetmaster- | 2016-03-31 06:50

yes, I have.
I think, it is because it rotates relative to viewport, not the character.
If you add Camera2d node to demo “Look at pointer”, you’ll see the same problem

JowannoSebastiano | 2016-03-31 10:40

:bust_in_silhouette: Reply From: ericdl

Have you tried get_global_mouse_pos() ?

extends Sprite

func _process(delta):
    var mpos = get_global_mouse_pos()
    look_at(mpos)

func _ready():
    set_process(true)

This one works better, thank you :slight_smile:
And can you explain me difference between:

get_local_mouse_pos() and get_global_mouse_pos()

?

JowannoSebastiano | 2016-04-05 09:51

local is the position relative to the parent

batmanasb | 2016-04-06 02:05

@batmanasb
and global…?

JowannoSebastiano | 2016-04-06 10:40

well, if you combine all the local positions up to the root node, that gets you the global (aka the global position), the position in the engine. Or get_global_position just does that for you.

Oh wait, I didn’t notice that you were talking about the mouse position. In that case local is the position in the viewport (from 0,0 to height,width), and global is the position in the actual game. So if the viewport (say a camera for example) was to move around but the mouse stood still, then the local mouse position wouldn’t change but the global would.

batmanasb | 2016-04-06 10:49

@batmanasb
thanks a lot, man.
May I ask you for your contact (twitter, e-mail, something) because i’m sure that I will have questions about godot?

JowannoSebastiano | 2016-04-06 16:52

it’s funny because you’ve already written my twitter handle every comment :stuck_out_tongue:

also, I’ve only been using Godot for about half a year so I’m no expert, but feel free to ask anyways

batmanasb | 2016-04-06 23:06

@batmanasb,
oh lol, you know, I’m so inattentive :smiley:
Because I’m jumpoffaith and already wrote you xD

JowannoSebastiano | 2016-04-07 07:18

small world :stuck_out_tongue:

batmanasb | 2016-04-07 08:04

This helped me allot.
But the problem with this code is that

get_global_mouse_pos()

Doesn’t work.
Instead use

get_local_mouse_position()

Someone might have pointed this out already but just in case.
non the less. thanks for the help :slight_smile:

Belistov | 2023-02-23 14:41