diff --git a/api/animate.js b/api/animate.js index d2973999..de3b8168 100644 --- a/api/animate.js +++ b/api/animate.js @@ -15,6 +15,11 @@ const determineDesiredShapeType = (animationName) => { return "vertical"; }; +const isOnSurface = (physicsMesh) => { + return typeof flock?.checkIfOnSurface === "function" && + flock.checkIfOnSurface(physicsMesh); +}; + const updateCapsuleShapeForAnimation = (physicsMesh, animationName) => { if ( !physicsMesh || @@ -28,7 +33,15 @@ const updateCapsuleShapeForAnimation = (physicsMesh, animationName) => { return; } - const desiredShapeType = determineDesiredShapeType(animationName); + let desiredShapeType = determineDesiredShapeType(animationName); + + // The horizontal capsule is designed for airborne movement. When a Fly + // animation is used on a surface (e.g. swimming), keep the vertical capsule + // so the character doesn't become unstable on slopes near the water's edge. + if (desiredShapeType === "horizontal-fly" && isOnSurface(physicsMesh)) { + desiredShapeType = "vertical"; + } + if (!physicsMesh.metadata) physicsMesh.metadata = {}; if (physicsMesh.metadata.currentPhysicsShapeType === desiredShapeType) {