Question about climbing mechanics in 3D

Godot Version

4.6.2

Question

I look around the internet for how people tend to approach a climbing mechanic, in this case, my objective is to be able to stick to a wall and slide up and down using raycasts to detect if the surface is climbable. I currently have some code as a result of trying to figure it out, but does not work properly.

A.) going against the surface the raycasts are supposed to detect doesn’t do much. I did this by setting the raycasts and the ladder to the same collision layer. The rays will turn red to indicate colliding, however when I go against the ladder, they do not seem to work the same. B.) I just don’t know if I’m doing the math right. I decided to try and add onto the y velocity of the player, I’m unsure how to have the transform be unaffected by gravity though, as when the rays are colliding I don’t want to be able to fall back down.

I hope this was clear enough. This is my script that handles climbing though:

class_name Climbing extends Node

@onready var player: CharacterBody3D = get_parent()
@onready var climb_ray: RayCast3D = %ClimbRay
@onready var climb_stop_ray: RayCast3D = %ClimbStopRay
var climbing := false
var climb_speed := 10

@onready var movement_component: MovementComponent = %MovementComponent



func _climbing(delta: float) -> void:	
	
	print(climbing)
	
	if climb_ray.is_colliding() and climb_stop_ray.is_colliding():
			climbing = true
			
	else:
		climbing = false

	if climbing == true:
		player.velocity.y += movement_component.direction.y * climb_speed * delta
		player.move_and_slide()

If the origin point of the ray enters the ladder’s collision shape the ray will stop detecting the ladder.

Why would gravity effect you? You’re not adding gravity to your y velocity. Are you adding gravity to your y velocity in a different node’s code?

To move on ladders and climb up ladders and walls, you:
1 - Detect collision
2 - Identify the normal to the ladder or wall and align your character to it
3 - Select the action that will start the climb and from there you will move up only
4 - Will keep checking for the wall and when it fails will proceed to continue movement, like advancing in the direction facing.
I played myself a lot with this and find a way that worked for FPS and TPS.
You can access my work at https://github.com/walterpalladino/godot-tps-controller