Godot Version
v4.7.stable.official [5b4e0cb0f]
Question
Hello! I’m fairly new to Godot and my first non-tutorial project is just a basic pong clone. For the player “paddles”, I am trying to generate 3 CollisionShape2D nodes on_ready() so that they can have different effects on the “puck” on collision. I have successfully generated the nodes on game start, but they are slightly up and to the left of where they are supposed to be (they are the transparent blue rectangles in the screenshot below, and should perfectly overlap the white rectangle):
I have checked in 2d view, and the top left point of the white rectangle (a ColorRect node) matches up with the position of its parent, PlayerPaddle(also the parent of the created CollisionShape2D nodes - the top node should line up with the PlayerPaddle position) :
Posting the entire player_paddle.gd script for completion, but I believe the issue would lie in thegenerate_collision_shapes()function. It takes the PADDLE_Y constant value, divides it by three, and uses that quotient to evenly space out the generated CollisionShape2D nodes along the y-axis.
extends Area2D
@onready var collision_shape_2d: CollisionShape2D = $CollisionShape2D
@onready var color_rect: ColorRect = %ColorRect
var puck_collision : Array[CollisionShape2D];
const PADDLE_X = 18
const PADDLE_Y := 75
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
generate_collision_shapes()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
var viewport_size := get_viewport_rect().size
if (Input.is_action_pressed("Paddle Up")) :
paddle_up(delta, viewport_size)
if (Input.is_action_pressed("Paddle Down")) :
paddle_down(delta, viewport_size)
func paddle_up(delta: float, _viewport: Vector2) -> void:
if position.y >= 0 :
position.y -= 150 * delta
func paddle_down(delta: float, viewport: Vector2) -> void:
if position.y <= viewport.y - collision_shape_2d.shape.size.y :
position.y += 150 * delta
func generate_collision_shapes() -> void:
var y_length = PADDLE_Y / 3
var y_point = 0
for index in 3 :
var new_collision = CollisionShape2D.new()
add_child(new_collision)
new_collision.shape = RectangleShape2D.new()
new_collision.position.y = y_point
new_collision.shape.size = Vector2(PADDLE_X, y_length)
print(new_collision.shape.size.x)
new_collision.add_to_group("paddle")
y_point += y_length
I had initially based the size of RectangleShape2D on the size of the ColorRect child node of PlayerPaddle, but when it didn’t work I swapped out for constant values early in the script thinking the values of ColorRect could be off. The offset issue appears to be exactly the same though. I also fiddled with different sizes for the RectangleShape2D nodes, and the X size always appears to be half off from what it should be. From what I understand, node positions default to be based off of their parent nodes, so I’m not really sure where to go from here.
Lastly, here is a screenshot of my screen tree:
Thanks for taking a look / any help you’re able to give!


