How can I lock the player's movement to a radius around the center of the screen?
I currently have a script setup for the player object to follow the mouse cursor, but I'm trying to get it to stay at a specified radius from the screen's center point, as well as rotate to face the center.
extends CharacterBody2D
const speed = 50
# Declare a variable to store the target position
var target = Vector2.ZERO
func _physics_process(delta):
# Get the mouse position in the global coordinate system
target = get_global_mouse_position()
# Get the direction vector from the character to the target
var direction = target - global_position
# Check if the direction vector is not zero
if direction.length() > 0:
# Normalize the direction vector and multiply by the speed
velocity = direction * speed
# Move the character and get the collision information
#var collision = move_and_slide()
# Check if the character has reached the target
if global_position.distance_to(target) < speed * delta:
# Stop the character
velocity = Vector2.ZERO
move_and_slide()