Having trouble with movement

Godot Version

4.2.2 stable

Question

So, I was having a issue with this basic pong clone game I was making. I am working on it just to get into coding and gdscript, but I am confused and have been scrolling the doc to find anything that could help but I can not. I am confused why my paddle won’t move, this is my code:
extends CharacterBody2D

@export var speed = 400
@export var can_move = true

func _physics_process(delta):
print(position)

if can_move == true:
	if Input.is_action_pressed("up"):
		position.y -= speed * delta
	if Input.is_action_pressed("down"):
		position.y += speed * delta

# Make sure the pong can move
if position > Vector2(-483, -255):
	can_move = true
elif position > Vector2(-483, 255):
	can_move = true

# Make sure the pong can't leave the screen
if position < Vector2(-483, -255):
	can_move = false
elif position < Vector2(-483, 255):
	can_move = false

Not sure, but using position.y to move is a little weird. Might be because you aren’t using velocity.y

Try changing your code to:

#velocity handles movement better I think
if can_move == true:
if Input.is_action_pressed(“up”):
velocity.y -= speed * delta
if Input.is_action_pressed(“down”):
velocity.y += speed * delta

#put this at the bottom of the physics process function
move_and_slide()

2 Likes

Thank you, I am just randomly scrolling the documentation and seeing what catches my eyes and figure out how to use whatever it could be. I am going to try to learn the best practices in GdScript which will be super hard but maybe it will prevent issues like this.