Https://www.gamedev.tv/courses/godot-complete-2d/lectures/46188360

	var screensize = get_viewport_rect().size
	screensize.x -= 100
	screensize.y -= 100
	global_position = global_position.clamp(Vector2(100,100),screensize)

lightweight way to clean up the edges of play window

	var bumper = 100
	var screensize = get_viewport_rect().size
	screensize.x -= bumper
	screensize.y -= bumper
	global_position = global_position.clamp(Vector2(bumper,bumper),screensize)
	var bumper_x = 55
	var bumper_y = 20
	var screensize = get_viewport_rect().size
	screensize.x -= bumper_x
	screensize.y -= bumper_y
	global_position = global_position.clamp(Vector2(bumper_x,bumper_y),screensize)

a final example

3 Likes

Your final example can be further simplified by using a Vector2 so that you are performing math operations directly between Vector2 variables. I would also suggest using the variable name padding, since that is the typical term to describe the space outside an object before it hits the border.

var padding = Vector2(55, 20)
var screen_size = get_viewport_rect().size
global_position = global_position.clamp(padding, screen_size - padding)
1 Like

Cool stuff! Keeping these lines commented for future reference, thanks fellas. :green_heart:

1 Like

Privacy & Terms