0 votes

I am testing an interaction mechanic with having the ability of dragging a box by left clicking. The problem is that whenever I left click, no matter where the box is, it will teleport to the location of where the mouse is. I think I figured it out by having an invisible sprite interacting with another object by colliding on the object's "interaction area" with the Area2D node.

But... that's where I ran into a problem:
How do you check on the position of different nodes without the node you want to interact with is a child node? I know how to check on for child nodes within a main node but a separate node? dunno.

Also, I know that this is not have to do with the question but if anyone that reads this question knows how to also get a node to detect if the mouse cursor is hovering over or knows a better solution to this problem that will help. ~^-^~

in Engine by (83 points)

1 Answer

0 votes

Objects can interact in many ways, that depends on the design.

In your case, click and box appears where is clicked, is not magic, you programmed it to do that, if you want to move slowly then you need to program that behaviour too.

get_pos or get_global_pos on 2D are used to get position, if you have a reference to a node (you can get it in many ways, depends on what you want to do and your design) just use those methods.

CollisionObjects (like Area) have _input_event, if they have mouse event is because the mouse is on the area.

There are other ways, you can use Physics2DDirectSpaceState intersect methods and test mouse position to check if there is something in that point.

Once you "picked" a CollisionObject, make it "follow" the mouse pointer.

There are 2 official demos that use areas and pickable bodies that you can look to see how works.

by (7,934 points)

Okay thanks! What about an object entering the area 2D? how do you check for that?

Areas can be used for overlap detection against other areas/bodies, so if the node has a child area (or is an area), the faster way is to use the area/body_enter signals.

An example, the player is a body and spikes an area, spikes can have a signal connected to something (or self), the _enter signals have a parameter with reference to the body that entered so in this case (after a type or group check) you can call a method on that body to "tell" it what happened.

On demos:

  • The princess on the kinematic character demos are areas and use a signal to display a label on body enter.
  • Coins on platformer demos are areas that use a flag to accept or ignore the signal and play an animation.
  • The ship on the simple shooter demo is an area that detects body enter (for tilemap collision) and area enter (for enemy, I think it should test group instead of method but is just preference).
  • Shower of bullets demo use a more advanced way manage a high number of physics elements.

Thanks! I'm trying out the method of having the Area2D check if the mouse enters and send a signal the main Node which I found to be more efficient than having an invisible object constantly follow a mouse but I'll keep this in mind once I start working with hazardous objects to send the signal to the player that the character is dead.

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 Frequently asked questions and 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 [email protected] with your username.