0 votes

I'm trying to make a function that moves the camera as far as the mouse moves when middle clicking (like how it works in Inkscape for example). Within a Camera2D script I have this code:

func move_camera():
   position + InputEventMouseMotion.get_relative()

When writing this I get the error in the title. I was wondering how to fix this error, or if there is a better way to approach what I want to accomplish.

Godot version 3.3.2
in Engine by (26 points)

1 Answer

+1 vote
Best answer

Hi,

First off, you are not modifying anything with your current code. What you should do to set the new position is: position = position + ...
Doing what you do now (position + ...) simply calculates the value of this sum and discards it right away.

Regarding the movement logic, I see there is a misconception on what InputEventMouseMotion means here.
InputEventMouseMotion is an extension of InputEvent, which are data containers that encapsulate user input events. The name InputEventMouseMotion is one of a type and not of a global object.
To intercept and react to InputEvents you should use the builtin _unhandled_input function:

func _unhandled_input(event):
    if event is InputEventMouseMotion:
        print("mouse moved by ", event.relative, " pixels.")

You can see the InputEvent docs here.

by (728 points)
selected by

Worked great. Thank you :)

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 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 webmaster@godotengine.org with your username.