Athsmooth
Athsmooth

Reputation: 45

Area2D Not Triggering Object in Godot

Here is my code: (just so you know I am a beginner and I just started this week, though I do have knowledge with other languages and game engines)

func _on_Area2D_area_entered(area):
    get_parent().get_node("Level 1/Area2D/Flag").rotation_degrees += 1

What I was trying to accomplish was that the Player GameObject would see if its in the area of the Flag, and if it is, the flag would rotate.

I am not sure where the issue is. I think it is probably in the second line. I provided a screenshot below if I did the setup wrong. I have looked at the other close questions asked on the same topic, but they did not answer my question.

enter image description here

The "Player" GameObject is the one with the script that contains the detection if its in the area2D.

Upvotes: 3

Views: 6249

Answers (1)

Theraot
Theraot

Reputation: 40220

If you want to check if the Area2D is positioned correctly during runtime enable Debug -> Visible Collision Shapes.

If you want to check if _on_Area2D_area_entered is running, add breakpoints (or use print).


Did you get an error?

If there isn't a Node there, this expression will cause an error in runtime:

get_parent().get_node("Level 1/Area2D/Flag")

If you want to be able to check, you can use get_node_or_null and is_instance_valid.

Since you didn't mention any error, I'm going to guess the method is not running.


If the method is not running, the most likely culprit is that - I'm guessing given then name of the method - you connected the "area_entered" signal but intended to connect the "body_entered" signal.

The "area_entered" signal will trigger when another Area2D enters the Area2D. But I only see one Area2D in your scene tree. On the other hand the "body_entered" will trigger when a PhysicsBody2D (e.g StaticBody2D, KinematicBody2D, RigidBody2D) enters the Area2D. In either case you get what entered as a parameter of the method.

Other reasons why the Area2D might not be detecting what you want include no intersection of collision_layer and collision_mask and monitoring being disabled.


And to dismiss a couple possible misconceptions:

  • The "area_entered" and "body_entered" trigger when the Area2D or PhysicsBody2D respectively enter the Area2D, not every frame they are inside. So rotation_degrees += 1 is not a rotation animation.
  • You will get notifications of anything that trigger the signals, not just the object to which you connected it. You may have to further filter, e.g. if body == self:.

For people arriving here from search, I want to link a similar case: Enemy is not affected by bullets. And also my full explanation of how to set up physic nodes.

Upvotes: 7

Related Questions