@@ -611,14 +611,18 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
611
611
/**
612
612
* Setup moving direction
613
613
*/
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
615
617
if (
616
618
actualSlopeAngle < slopeMaxAngle &&
617
619
Math . abs ( slopeAngle ) > 0.2 &&
618
620
Math . abs ( slopeAngle ) < slopeMaxAngle
619
621
) {
620
622
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 ) {
622
626
movingDirection . set (
623
627
0 ,
624
628
Math . sin ( slopeAngle ) > 0 ? 0 : Math . sin ( slopeAngle ) ,
@@ -753,6 +757,11 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
753
757
if ( getCameraBased ( ) . isCameraBased ) {
754
758
modelEuler . y = pivot . rotation . y
755
759
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 ) )
756
765
} else {
757
766
characterModelIndicator . getWorldDirection ( modelFacingVec )
758
767
}
@@ -969,9 +978,10 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
969
978
useFrame ( ( state , delta ) => {
970
979
if ( delta > 1 ) delta %= 1 ;
971
980
972
- // Character current position
981
+ // Character current position/velocity
973
982
if ( characterRef . current ) {
974
983
currentPos . copy ( characterRef . current . translation ( ) as THREE . Vector3 ) ;
984
+ currentVel . copy ( characterRef . current . linvel ( ) as THREE . Vector3 ) ;
975
985
// Assign userDate properties
976
986
( characterRef . current . userData as userDataType ) . canJump = canJump ;
977
987
( characterRef . current . userData as userDataType ) . slopeAngle = slopeAngle ;
@@ -1031,10 +1041,6 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
1031
1041
if ( forward || backward || leftward || rightward || gamepadKeys . forward || gamepadKeys . backward || gamepadKeys . leftward || gamepadKeys . rightward )
1032
1042
moveCharacter ( delta , run , slopeAngle , movingObjectVelocity ) ;
1033
1043
1034
- // Character current velocity
1035
- if ( characterRef . current )
1036
- currentVel . copy ( characterRef . current . linvel ( ) as THREE . Vector3 ) ;
1037
-
1038
1044
// Jump impulse
1039
1045
if ( ( jump || button1Pressed ) && canJump ) {
1040
1046
// characterRef.current.applyImpulse(jumpDirection.set(0, 0.5, 0), true);
0 commit comments