Keep the Paddle on Screen
Part of the Projects section of Coddy's Godot journey. Lesson 2 of 33.
Hold Right and the paddle slides off the screen. clamp(value, low, high) keeps a number between two limits. Below low it gives low. Above high it gives high. Otherwise it gives the number unchanged.
position.x = clamp(position.x, 40, 600)The paddle is 80 pixels wide. At x = 40 its left side touches the left edge. At x = 600 its right side touches the right edge, at 640.
Clamp after the move. If you clamp first, the move can push the paddle past the edge.
Challenge
EasyKeep the paddle fully on the screen: after it moves, clamp position.x between 40 and 600.
Try it yourself
extends CharacterBody2D
const SPEED = 400
func _physics_process(delta):
var direction = Input.get_axis("move_left", "move_right")
position.x += direction * SPEED * delta
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
1Project: Breakout
Breakout PaddleKeep the Paddle on ScreenPractice on your own: Godot playground