The player can't move up in ping pong game

Godot Version


so I followed this tutorial on youtube
and for some reason I can’t make the player move up

here’s the code

extends StaticBody2D

@export var speed = 5000

var win_hight : int
var p_hight : int

# Called when the node enters the scene tree for the first time.
func _ready():
	win_hight = get_viewport_rect().size.y
	p_hight = $ColorRect.get_size().y

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Input.is_action_pressed("ui_up"):
		position.y -= get_parent().Paddle_Speed * delta
	if Input.is_action_pressed("ui_down"):
		position.y += get_parent().Paddle_Speed * delta
	position.y = clamp(position.y, p_hight/2, win_hight - p_hight/2)

here’s a video about the glitch

Hmmm, Something wrong with your this code:

position.y = clamp(position.y, p_hight/2, win_hight - p_hight/2)

Therefore tell me the value of p_height and win_height

their values are int (I didn’t put specific value in it just the variable int)

also, the player position is -512 in x axis and 0 on y axis

Ok, try to print p_hight/2 and win_hight - p_hight/2

I got this numbers

324 for win_hight
60 for p_hight

588 for win_hight - p_hight/2

means p_hight/2 = 60? and default player position y is 0?

yes the p_hight/2 = 60 and the default player position on y axis is 0

Screen Size???, your codes is wrong, do like this:

position.y = clamp(position.y, -(screen_size_y/2), screen_size_y)

wait, I have edited my reply, again check this upper code you properly wrote in your script

screen size for the game is

width 1152
height 648

Ok, replace your that code with my code

well, it can move up but it can pass the upper border, anyways thanks for helping

Ok, so do like this

position.y = clamp(position.y, -(screen_size_y/2)+paddle.size.y, (screen_size_y/2)-paddle.size.y)

what’s paddle ??

Player, the texture rect or sprite you used for the white rectangle as player

aha okay buddy, thx for info

1 Like