Make RigidBody2D bounce only in the Y axis

Godot Version

4

Question

Hello, I’m trying to make a RigidBody2D bounce non-stop only in the Y axis (it should not move in the X axis), to create sort of an obstacle area where there’ll be multiple rigidbody2d’s bouncing so the player need to evade them, but I can’t get the body to constantly bounce because it loses force and I’m not fully getting the grasp of how the physics system works in godot, any help will be welcomed, thanks.
This is the code I used but when the body touches a wall it stays static for like a second and then completly bugs out of screen.

var speed = 500
var velocity = Vector2.ZERO

@onready var sprite: Sprite2D = $Sprite2D

func _physics_process(delta):
	var collision_obj = move_and_collide(velocity * speed * delta)
	if collision_obj:
		collision_obj = move_and_collide(-velocity * speed * delta)

Hi,
I think you’re only supposed to use move_and_collide the once.
I’d suggest that afterwards you just want to change the velocity, e.g. something like

if collision_obj:
    velocity.x = 0
    velocity.y = -velocity.y

although I’m guessing as to how you’d want to make the body “bounce” based on the title “bounce only in the Y axis”.