Get Global Mouse Position

Get Global Mouse Position
Godot 4.1.2

Hello, I am new to Godot and I would like a question answered, my character is static and I want his weapon to point in the direction of my mouse at all times, but I am having problems since I saw the documentation and I understand that you can use these options:
1-var mouse_pos = get_global_mouse_pos()
2-get_viewport().get_global_mouse_position()

The first option tells me that it is not found, the second does “work” but it does nothing.

This is the small code that I have as proof, the weapon rotates from a 2d node called Pivot:

extend Node

func _ready():
pass # Replace with function body.

func _physics_process(delta):
look_at_mouse

func look_at_mouse():
var mouse_pos = get_viewport().get_global_mouse_position()
get_node(“Pivot”).look_at(mouse_pos)

.__.
I had to use “extends Node2D”

I’ll still leave this Topic in case someone like me needs it

1 Like

Node2D works because get_global_mouse_position() is a CanvasItem function.
https://docs.godotengine.org/en/stable/classes/class_canvasitem.html#class-canvasitem-method-get-global-mouse-position
Node2D Inherits from CanvasItem, but Viewport doesn’t.

In the docs under (most) node descriptions there are a list of inherits/inherited by. A node can use values, functions, etc… from nodes that it inherits from.

1 Like

I’ll keep that in mind, thank you :handshake: