Another method of clamping player position

I tried my hand at stopping the player position before seeing how it was done in the tutorial and this was the method I came up with to avoid directly changing the player position outside of move_and_slide()

	if global_position.x <= 0 && velocity.x == -SPEED:
		velocity.x = 0
	if global_position.x >= 1280 && velocity.x == SPEED:
		velocity.x = 0
	if global_position.y <= 0 && velocity.y == -SPEED:
		velocity.y = 0
	if global_position.y >= 720 && velocity.y == SPEED:
		velocity.y = 0

quick edit:

I changed it a tiny bit so if I decide to increase the viewport size later I don’t need to come back and find this code again

	if global_position.x <= 0 && velocity.x == -SPEED:
		velocity.x = 0
	if global_position.x >= ProjectSettings.get_setting("display/window/size/viewport_width") && velocity.x == SPEED:
		velocity.x = 0
	if global_position.y <= 0 && velocity.y == -SPEED:
		velocity.y = 0
	if global_position.y >= ProjectSettings.get_setting("display/window/size/viewport_height") && velocity.y == SPEED:
		velocity.y = 0

another edit:

I see my previous edit was kinda adressed further in the video lol

1 Like

Privacy & Terms