Skip to content

Commit 464cb75

Browse files
fix: update slope ray position in fps mode
1 parent 5eeaa71 commit 464cb75

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ecctrl",
3-
"version": "1.0.75",
3+
"version": "1.0.76",
44
"author": "Erdong Chen",
55
"license": "MIT",
66
"description": "A floating rigibody character controller for R3F",

src/Ecctrl.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -611,14 +611,18 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
611611
/**
612612
* Setup moving direction
613613
*/
614-
// Only apply slope extra force when slope angle is between 0.2 and slopeMaxAngle, actualSlopeAngle < slopeMaxAngle
614+
// Only apply slope angle to moving direction
615+
// when slope angle is between 0.2rad and slopeMaxAngle,
616+
// and actualSlopeAngle < slopeMaxAngle
615617
if (
616618
actualSlopeAngle < slopeMaxAngle &&
617619
Math.abs(slopeAngle) > 0.2 &&
618620
Math.abs(slopeAngle) < slopeMaxAngle
619621
) {
620622
movingDirection.set(0, Math.sin(slopeAngle), Math.cos(slopeAngle));
621-
} else if (actualSlopeAngle >= slopeMaxAngle) {
623+
}
624+
// If on a slopeMaxAngle slope, only apply small a mount of forward direction
625+
else if (actualSlopeAngle >= slopeMaxAngle) {
622626
movingDirection.set(
623627
0,
624628
Math.sin(slopeAngle) > 0 ? 0 : Math.sin(slopeAngle),
@@ -753,6 +757,11 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
753757
if (getCameraBased().isCameraBased) {
754758
modelEuler.y = pivot.rotation.y
755759
pivot.getWorldDirection(modelFacingVec)
760+
// Update slopeRayOrigin to new positon
761+
const slopeRayOriginNewPosition = new THREE.Vector3(movingDirection.x, 0, movingDirection.z)
762+
const crossVecOnY = slopeRayOriginNewPosition.clone().cross(modelFacingVec)
763+
slopeRayOriginRef.current.position.x = slopeRayOriginOffest * Math.sin(slopeRayOriginNewPosition.angleTo(modelFacingVec) * (crossVecOnY.y < 0 ? 1 : -1))
764+
slopeRayOriginRef.current.position.z = slopeRayOriginOffest * Math.cos(slopeRayOriginNewPosition.angleTo(modelFacingVec) * (crossVecOnY.y < 0 ? 1 : -1))
756765
} else {
757766
characterModelIndicator.getWorldDirection(modelFacingVec)
758767
}
@@ -969,9 +978,10 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
969978
useFrame((state, delta) => {
970979
if (delta > 1) delta %= 1;
971980

972-
// Character current position
981+
// Character current position/velocity
973982
if (characterRef.current) {
974983
currentPos.copy(characterRef.current.translation() as THREE.Vector3);
984+
currentVel.copy(characterRef.current.linvel() as THREE.Vector3);
975985
// Assign userDate properties
976986
(characterRef.current.userData as userDataType).canJump = canJump;
977987
(characterRef.current.userData as userDataType).slopeAngle = slopeAngle;
@@ -1031,10 +1041,6 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
10311041
if (forward || backward || leftward || rightward || gamepadKeys.forward || gamepadKeys.backward || gamepadKeys.leftward || gamepadKeys.rightward)
10321042
moveCharacter(delta, run, slopeAngle, movingObjectVelocity);
10331043

1034-
// Character current velocity
1035-
if (characterRef.current)
1036-
currentVel.copy(characterRef.current.linvel() as THREE.Vector3);
1037-
10381044
// Jump impulse
10391045
if ((jump || button1Pressed) && canJump) {
10401046
// characterRef.current.applyImpulse(jumpDirection.set(0, 0.5, 0), true);

0 commit comments

Comments
 (0)