diff --git a/package-lock.json b/package-lock.json index cc8bf3e..179c70f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ecctrl", - "version": "1.0.75", + "version": "1.0.76", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ecctrl", - "version": "1.0.75", + "version": "1.0.76", "license": "MIT", "dependencies": { "@react-spring/three": "^9.7.3", diff --git a/package.json b/package.json index 59d173b..eb91806 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ecctrl", - "version": "1.0.75", + "version": "1.0.76", "author": "Erdong Chen", "license": "MIT", "description": "A floating rigibody character controller for R3F", diff --git a/src/Ecctrl.tsx b/src/Ecctrl.tsx index 1245126..7599cc7 100644 --- a/src/Ecctrl.tsx +++ b/src/Ecctrl.tsx @@ -611,14 +611,18 @@ const Ecctrl: ForwardRefRenderFunction = ({ /** * Setup moving direction */ - // Only apply slope extra force when slope angle is between 0.2 and slopeMaxAngle, actualSlopeAngle < slopeMaxAngle + // Only apply slope angle to moving direction + // when slope angle is between 0.2rad and slopeMaxAngle, + // and actualSlopeAngle < slopeMaxAngle if ( actualSlopeAngle < slopeMaxAngle && Math.abs(slopeAngle) > 0.2 && Math.abs(slopeAngle) < slopeMaxAngle ) { movingDirection.set(0, Math.sin(slopeAngle), Math.cos(slopeAngle)); - } else if (actualSlopeAngle >= slopeMaxAngle) { + } + // If on a slopeMaxAngle slope, only apply small a mount of forward direction + else if (actualSlopeAngle >= slopeMaxAngle) { movingDirection.set( 0, Math.sin(slopeAngle) > 0 ? 0 : Math.sin(slopeAngle), @@ -753,6 +757,11 @@ const Ecctrl: ForwardRefRenderFunction = ({ if (getCameraBased().isCameraBased) { modelEuler.y = pivot.rotation.y pivot.getWorldDirection(modelFacingVec) + // Update slopeRayOrigin to new positon + const slopeRayOriginNewPosition = new THREE.Vector3(movingDirection.x, 0, movingDirection.z) + const crossVecOnY = slopeRayOriginNewPosition.clone().cross(modelFacingVec) + slopeRayOriginRef.current.position.x = slopeRayOriginOffest * Math.sin(slopeRayOriginNewPosition.angleTo(modelFacingVec) * (crossVecOnY.y < 0 ? 1 : -1)) + slopeRayOriginRef.current.position.z = slopeRayOriginOffest * Math.cos(slopeRayOriginNewPosition.angleTo(modelFacingVec) * (crossVecOnY.y < 0 ? 1 : -1)) } else { characterModelIndicator.getWorldDirection(modelFacingVec) } @@ -969,9 +978,10 @@ const Ecctrl: ForwardRefRenderFunction = ({ useFrame((state, delta) => { if (delta > 1) delta %= 1; - // Character current position + // Character current position/velocity if (characterRef.current) { currentPos.copy(characterRef.current.translation() as THREE.Vector3); + currentVel.copy(characterRef.current.linvel() as THREE.Vector3); // Assign userDate properties (characterRef.current.userData as userDataType).canJump = canJump; (characterRef.current.userData as userDataType).slopeAngle = slopeAngle; @@ -1031,10 +1041,6 @@ const Ecctrl: ForwardRefRenderFunction = ({ if (forward || backward || leftward || rightward || gamepadKeys.forward || gamepadKeys.backward || gamepadKeys.leftward || gamepadKeys.rightward) moveCharacter(delta, run, slopeAngle, movingObjectVelocity); - // Character current velocity - if (characterRef.current) - currentVel.copy(characterRef.current.linvel() as THREE.Vector3); - // Jump impulse if ((jump || button1Pressed) && canJump) { // characterRef.current.applyImpulse(jumpDirection.set(0, 0.5, 0), true);