Godot Version
v4.2.2.stable.official [15073afe3]
Question
Hello,
I am making pong. My ball is a CharacterBody2D scene. My player and cpu paddles are StaticBody2D scenes. I have instantiated them all into my game scene. In the game scene I have a node called TopBotBorder which is a StaticBody2D as well that has two CollisionShape2D children to act as the top and bottom borders.
Within my ball script I have referenced my player and cpu scenes as follows.
@onready var Player = get_tree().get_first_node_in_group("player")
@onready var CPU = get_tree().get_first_node_in_group("cpu")
Then in my _physics_process(delta) function I have tried to make a variable called collision (to handle the move_and_collide), a variable called collider (to denote what object collided with the ball), and an if statement to try and handle the case of if I hit the player, the cpu, or the TopBotBorder. As noted below.
func _physics_process(delta):
var collision = move_and_collide(ballDirection*speed*delta)
var collider
if collision:
collider = collision.get_collider().name
# if ball hits player or cpu paddle
if collider == Player.name or CPU.name:
speed += ballAccel
print(collision.get_collider().name)
ballDirection=ballDirection.bounce(collision.get_normal())
#if ball hits TopBotBorder
else:
ballDirection=ballDirection.bounce(collision.get_normal())
In my output it is printing TopBotBorder when it hits a TopBotBorder, Player when it hits the player paddle, and CPU when it hits the cpu paddle. I can’t seem to get it to be selective for just the player or cpu paddle in that if statement.
Thanks for your time. Also, this is my first ever post so if I’ve done some formatting or etiquette error please provide feedback.