Need help with Pong enemy

Godot Version

4.5

Question

Hi, this is like my 5th day learning Godot and code

I’m trying to recreate pong but cant figure out how to get the enemy paddle to even move, I tried
if Ball_pos.y < position.y:
position.y += speed * _delta

but the paddle either kept ramming the top of the screen or when the opposite was put in it just went through the bottom of the screen, now it just does nothing. Any help would be appreciated as i’m clueless as to what to do after looking at documentation and searching stuff up. Thanks in advance

extends CharacterBody2D


@export var speed = 200

var screen_width = get_viewport_rect().size.x
var screen_middle= screen_width/2
@onready var Ball_pos = $"../Ball".position

func _physics_process(_delta: float) -> void:
	
	if Ball_pos.y < position.y:
		print('less than')

If you’re using CharacterBody2D, you need to call move_and_slide or move_and_collide at the end of your _physics_process, you shouldn’t manually set the position of the paddle.
There’s a really nice explanation on how to use it in the documentation:

I believe the reason nothing happens is this line:

@onready var Ball_pos = $"../Ball".position

@onready runs once when the scene loads, so Ball_pos is a copy of where the ball was on the first frame and it never updates after that. Your paddle is comparing against an old position forever. Store the ball node instead and read its position each frame:

@onready var ball = $"../Ball"

func _physics_process(delta: float) -> void:
	if ball.position.y < position.y:
		position.y -= speed * delta
	elif ball.position.y > position.y:
		position.y += speed * delta
	position.y = clamp(position.y, 0, get_viewport_rect().size.y)

Also note the direction. In Godot y increases downward, so when the ball is above the paddle you subtract to move up. Your first version added, which is why it rammed the top of the screen. The clamp line is what keeps it from flying off the bottom like your other attempt.

That worked, Thank you so much!