0 votes


i am looking for an efficient way to make some 2D sprites clickable / react to mouse inputs. I found this code snippet online:

extends Sprite

func unhandledinput(event):
if event is InputEventMouseButton and event.pressed and not event.isecho() and event.buttonindex == BUTTONLEFT:
var pos = position + offset - ( (texture.get
size() / 2.0) if centered else Vector2() )
if Rect2(pos, texture.getsize()).haspoint(event.position):


I tested this with an centered 64x64 rectangle as a sprite. The code itself worked, but unfortunately the Rect2 object created in the script is a bit off on the y-axis and does not fit the sprite. I tried to fix this by manually adjusting the position of the Rect2 object, but the adjustment would have to be made for each other sprite individually.

Do you know a solution to this problem or a better way to select sprites per mouse click?

in Engine by (37 points)

1 Answer

+1 vote

Use an Area2D. You get input handling with CollisionObject2D signals like input_event and mouse_entered/exited.

Alternatively, use a TextureRect and you get all the Control node input handling.

by (20,679 points)
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.