Godot Version
4.2
Area 2D not Detecting Rigidbody 2D or triggering body_entered signal
Hello. My apologies if this topic is basic, but this is my second day working with Godot. After completing the “Your First Game” in the documentation, I attempted to make a Pong game.
Paddle 1 moves and is controlled by the user (works as expected)
Paddle 2 follows the ball (most of the time)
The ball moves in a random direction at the start (also works)
However, the ball does not bounce off either paddle
Paddle, Paddle2, and Ball are all separate scenes that were loaded into the main scene which is why their children are yellow in the attached image (I think)
What I’m attempting to do:
func _on_body_entered(_body):
hit.emit()
hide() #Visual confirmation that the signal was triggered
“hit” is a signal that is picked up by the Main node which does this:
func _process(_delta):
if $Ball != null:
$Paddle2.ball = $Ball.position
$Paddle2.ballocity = $Ball.velocity
func _on_paddle_hit():
$Ball.directional = -1
print("This statement works")
func _on_paddle_2_hit():
$Ball.directional = -1
print("This statement works")
The code in _process works, and it’s what controls the motion of Paddle 2 (which does function as expected - at least most of the time)
Which is why I expected $Ball.directional = -1 to work as well. What does directional do?
Ball:
func _process(delta):
position += velocity * delta
position = position.clamp(Vector2.ZERO, screen_size)
if position[1] >= (screen_size[1] * 0.96) || position[1] <= (screen_size[1] * 0.04):
velocity.y *= -1
if position[0] <= (screen_size[0] * 0.03):
hide()
if directional == -1:
velocity.x *= directional
directional = 1
It simply inverts the x component of the ball’s velocity (and is reassigned to the value 1 as it was originally defined)
I originally tried doing something else in main
$Ball.velocity.x *= -1
But when that didn’t work I tried the approach of manipulating other variables instead, because it worked for Paddle #2
I’m really at a loss, even after reading the docs, double checking the scripts in the “Your First Game” project (which also uses an Area2D to detect a Rigidbody2D), and that works but this doesn’t and the difference is… nothing? I’m just really confused.
And yes, The monitoring property of Area2D is on, the signal is connected, and they are set to the same collision layer of 1, with a mask of 1 for Area2D. Which is why I’m really confused as to why the collision isn’t triggering at all.
I will answer any other questions ASAP and I would really appreciate the help.
Thank you for your time