Aligning movement around a 2D circle

Godot Version

Godot 4.3

Question

I was wondering how I could align a character to move around a 2D circle, sorta like mario galaxy but in 2D. There seem to be an outdated tutorial about it but I’m unable to turn it into my version of godot

What specifically is not working from the older tutorial?

1 Like

move_and_slide_with_snap() is no longer a thing.

If you look at this discussion, they talk about how to get around this issue:
https://godotforums.org/d/32724-what-replaces-move-and-slide-with-snap-in-godot-4

TLDR:

Increase floor snap lenght of the characterbody, while using move_and_slide()

1 Like

Thank you for your time. I messed around with it but couldn’t get it to work like the tutorial.

For future readers. I ended up using raycasts. One going down which is all you need but in my case I added another pointing up to re-orient the character if they were close to another surface while jumping.

extends CharacterBody2D
 
const SPEED: float = 350
const GRAVITY: float = 450
const JUMP: float = -400
var air_time = 0
var is_jumping = false
var raw_velocity: Vector2
 
@onready var floor_detector: RayCast2D = $FloorDetector as RayCast2D
@onready var up_detector: RayCast2D = $UpDetector as RayCast2D



func _physics_process(delta: float) -> void:
	raw_velocity.x = Input.get_axis("ui_left", "ui_right") * SPEED
	if Input.is_action_just_pressed("Jump"):
		is_jumping = true
	
	if is_on_floor() and !is_jumping:
		raw_velocity.y = 0.0
	elif is_jumping:
		air_time += delta
		up_direction = Vector2(0,-1)
		if air_time > 0.2:
			is_jumping = false
			air_time =0
		raw_velocity.y = JUMP
	else:
		raw_velocity.y = GRAVITY
 	
	if floor_detector.is_colliding():
		var normal = floor_detector.get_collision_normal()
		up_direction = normal
 
		var angle = Vector2.UP.angle_to(normal)
		rotation = angle
 
		velocity = raw_velocity.rotated(angle)
	elif up_detector.is_colliding():
		var normal = up_detector.get_collision_normal()
		up_direction = normal
		var angle = Vector2.UP.angle_to(normal)
		rotation = angle
	else:
		velocity = raw_velocity
 	
	
	move_and_slide()

This allows for moving all around an inner surface. Useful for platformers if you want to make, say, sticky boots upgrade or something? if someone managed to remake the tutorial i posted above, feel free to post it as well for future readers.