Help with ShapeCast3D Collision and Sliding

Godot Version

4.4

Question

Hi! I recently decided to make my own Character Controller from scratch and I came to a problem of collisions. I used this tutorial and came back with this function:

func collideAndSlide(distanceToTravel : Vector3, depth : int):
	if (depth >= MAX_BOUNCES):
		return Vector3.ZERO
	
	var distance : float = distanceToTravel.length() + SKIN_THICKNESS
	
	collider.target_position = distanceToTravel.normalized() * distance
	collider.force_shapecast_update()
	if collider.is_colliding():
		var hitDistance : float = distance * collider.get_closest_collision_safe_fraction()
		var surfaceSnap : Vector3 = distanceToTravel.normalized() * (hitDistance - SKIN_THICKNESS)
		
		var leftoverTravel : Vector3 = distanceToTravel - surfaceSnap
		
		if surfaceSnap.length() <= SKIN_THICKNESS:
			surfaceSnap = Vector3.ZERO
		
		for i in range(collider.get_collision_count()):
			var normalPlane : Plane = Plane(collider.get_collision_normal(i))
			leftoverTravel = normalPlane.project(leftoverTravel)
			velocity = normalPlane.project(velocity)
		
		print(leftoverTravel)
		
		return surfaceSnap + collideAndSlide(leftoverTravel,depth+1)
	
	return distanceToTravel

And while this does work, it has several small bugs surrounding it that I am unsure of how to solve. In this video example you can see how it jitters when it sliding into a corner, how the physics are seemingly random and slow-down when moving against the wall and the margin often bugs and you can see how the collider is a certain distance away before sliding, but then suddenly when you go at an angle into a wall it snaps you closer.

So, what I learnt is that ShapeCast’s aren’t accurate which is why the margin error was happening so I’m looking into new ways of implementing this.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.