Godot Version
Godot 4.1.1
Question
Collision with specific nodes?
Hello, I am new to Godot. I intended on making a momentum based platformer like Sonic but it quickly occurred to me I have no clue what I’m doing so I need to go way smaller.
So I decided to make Pong as a learning experience. I’ve managed to get the bats and balls working, it’s the score system I’m having trouble with.
I want to have the ball go off screen then one of the players gain score. Only issue is I don’t have the faintest clue how to code this. I have the score area for Player 2 in the game, issue is the ball just bounces back and doesn’t change the script.
Is there a way to have collision detection with a specific CollisionBody2D node? I’d love to learn how to implement this.
Thank you.
Oldschool pong only did collision between the ball and the paddles; the rest was determined by screen position. If the ball was going to go off the top or bottom of the screen they (IIRC) negated the Y component of the vector (so it would “bounce”), and if it was going off the left or right side of the screen the appropriate player gets a point.
Something like:
func ball_move():
ball.position += ball_vector
if ball.position.y < 0 || ball.position > SCREEN_Y:
ball_vector.y = -ball_vector.y
if ball.position.x < 0:
player_1_score += 1
new_serve()
elif ball.position > SCREEN_X:
player_1_score += 1
new_serve()
If you want to do this with collision:
- the layers let you decide what collides with what
- you can have the different colliders call different handlers:
func entered_p1_score_area(_body):
player[0].score()
new_serve()
func entered_p2_score_area(_body):
player[1].score()
new_serve()
func wall_bounce(_body):
ball.vector.y = -ball.vector.y