Clamping CharacterBody3D Movement to Screen

Godot Version

4.2.1

Question

Hey everyone, I’m fairly new to the community but very happy to be here!

I’m also new to GD Script and have been trying to do a simple 3D character controller.
I’m essentially trying to make a character fly forward automatically through space and move on the X and Y axis. Think Start Fox 64!

The issue I’m having is that I’m struggling to clamp the character to the screen and stopping them fly off the screen when they reach the edge. This is what I’m currently attempting but to be honest I’m not sure it’s the correct way to do things!

#Screen clamping
global_position = global_position.clamp(Vector3.ZERO, get_viewport().size)

Does anyone have a solution for this?

Thank you!