+1 vote

I am working on a game and i want a sprite to change animation if the mouse pointer is hovering over an Area2D which is the child node of the sprite. The sprite is a closed door
and i want it to change to an open door.
If there is any other method to do this kindly tell.
I cannot find anything related to this question

in Engine by (16 points)

2 Answers

+5 votes
Best answer

There are a couple different ways to do this from what I could find.

The Area2D inherits from CollisionObject2D which has the signal mouse_entered From the docs:

Emitted when the mouse pointer enters any of this object's shapes. Requires inputpickable to be true and at least one collisionlayer bit to be set.

So make sure you set your input_pickable to on in the inspector and have a collision layer selection.

Check out the docs on how to setup signals.

For me, I was doing the checking on the mouse every frame and didn't want to have to connect my objects I was creating at run time, so on my master mouse script I used the Physics2DDirectSpaceState's method intersect_point:

func _physics_process(delta):
    # get the Physics2DDirectSpaceState object
    var space = get_world_2d().direct_space_state
    # Get the mouse's position
    var mousePos = get_global_mouse_position()
    # Check if there is a collision at the mouse position
    if space.intersect_point(mousePos, 1, ):
        print("hit something")
    else:
        print("no hit")

It's worth noting that the above code checks for Rigidbodies by default, if you need to check for Areas, you'll need to use some like

 if space.intersect_point(mousePos, 1, [], 2147483647, true, true):

The docs on the intersect_point method give more info on what all the params are, but the last true is the most impotant for Area2D checking.

by (54 points)
selected by
+1 vote

Hi,
Maybe look elsewhere: Detecting if the mouse is over an Area2D

by (1,817 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.