Keep the Paddle on Screen
Part of the Projects section of Coddy's Godot journey. Lesson 2 of 36.
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.
- In
paddle.gd, after the paddle moves, clampposition.x. - Keep it between
40and600.
Press Play and hold Right: the paddle stops at the edge. Then Check.
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
All lessons in Projects
1Project: Breakout
Breakout PaddleKeep the Paddle on ScreenPractice on your own: Godot playground