I have been trying to implement a top down version of the collide and slide algorithm, similar to the move_and_slide() in godot, insprided from this video:
But the problem I am having is that when going to a wide angle wall I get jitters.
private Vector3 CollideAndSlide(Vector3 vel, Vector3 pos, int depth) {
if (depth >= MaxBounces) {
return Vector3.zero;
}
float radius = 0.5f;
Vector3 dir = vel.normalized;
float dist = vel.magnitude + SkinWidth;
if (Physics.SphereCast(pos, (radius - SkinWidth), dir, out var hit, dist)) {
Vector3 snapToSurface = dir * (hit.distance - SkinWidth);
Vector3 leftOver = vel - snapToSurface;
if (snapToSurface.magnitude <= SkinWidth) {
snapToSurface = Vector3.zero;
}
float scale = 1 - Vector3.Dot(
new Vector3(hit.normal.x, hit.normal.y, 0f).normalized,
- new Vector3(vel.x, vel.y, 0f).normalized
);
float mag = leftOver.magnitude;
leftOver = Vector3.ProjectOnPlane(leftOver, hit.normal).normalized;
print(leftOver);
leftOver *= mag;
// leftOver *= scale;
return snapToSurface + CollideAndSlide(leftOver, pos + snapToSurface, depth + 1);
}
return vel;
}
When I uncomment the “leftOver *= scale;” part the corner jitter dissappears but then I get slowing of movement when sliding across. If I don’t want that the jitter comes back. I would really appreciate any help.