Getting a Vector2 from mouse motion relative to last frame

Godot Version



Hi, I’m making a player character that moves with the mouse cursor, using:

position = get_global_mouse_position()

I would like to get the mouse movement relative to the previous frame as a Vector2, since this would allow me to control the sprite animations, like this for example:

	if direction.x > 0:"steering_right")

From the Docs, i found this page InputEventMouseMotion — Godot Engine (stable) documentation in English

Basically it looks like i can use InputEventMouseMotion.relative to get a Vector2, but when i try to do it like this:

var direction := Vector2.ZERO

func _process(delta: float) -> void:
	direction = InputEventMouseMotion.relative

The compiler doesn’t complain, but when I run the game, I immediately get this red error:

Invalid get index 'relative' (on base:'InputEventMouseMotion').

I also tried:

func _process(delta: float) -> void:
	direction = InputEventMouseMotion.get_relative()

but i get an error saying:

Cannot call non-static function "get_relative()" on the class "InputEventMouseMotion" directly. Make an instance instead.

I don’t know what these two errors mean.
Any help?

InputEvent is an object which tells us what happened in inputs on a frame, so we need an instance to deal with.

We can get it in _input(InputEvent event) for example (I think this function is called whenever InputEvent object is instanciated). There is _gui_input(InputEvent event) too but it is about events happened on a control node.

1 Like

I got it to work (badly) using:

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		direction = event.relative.x

not satisfied with the result, but might be helpful for anyone wondering.


What do you mean exactly when you say “we need an instance to deal with”?

I am trying to use the same InputEventMouseMotion.relative Vector2 value for my game items to know where the mouse is located, and to then run another function that will move them to my cursor to collect them.

However, I am just not wrapping my head around the idea behind what InputEventMouseMotion is, or how to handle InputEvents in general.

Could you possible explain this to me?

The following is my understanding, which may be incorrect.

In godot, when an input event happens (mouse moved, mouse clicked, etc.), an InputEvent object is created. It holds the information what happened. It is eventually passed to functions like _input(event).

In this function you have event so you can see the contents of the event. One point here is that _input(event) would be called for any input event, so we need to filter as in mq95’s post.

We can (should) not get any instance of event directly from InputEvent class itself.

There are two more points.

  • Input class is confusing. This is somewhat static so you can see directly what input is given.
  • If you want mouse position only, you can use get_{global, local}_mouse_position() function.
1 Like