5
5
CapsuleCollider ,
6
6
useRapier ,
7
7
RapierRigidBody ,
8
+ type RigidBodyProps ,
8
9
} from "@react-three/rapier" ;
9
10
import { useEffect , useRef , useMemo , type ReactNode } from "react" ;
10
11
import * as THREE from "three" ;
@@ -55,7 +56,8 @@ export default function Ecctrl({
55
56
dampingC = 0.08 ,
56
57
// Slope Ray setups
57
58
showSlopeRayOrigin = false ,
58
- slopeRayOriginOffest = capsuleRadius ,
59
+ slopeMaxAngle = 1 , // in rad
60
+ slopeRayOriginOffest = capsuleRadius - 0.02 ,
59
61
slopeRayLength = capsuleRadius + 3 ,
60
62
slopeRayDir = { x : 0 , y : - 1 , z : 0 } ,
61
63
slopeUpExtraForce = 0.1 ,
@@ -67,6 +69,8 @@ export default function Ecctrl({
67
69
autoBalanceDampingOnY = 0.02 ,
68
70
// Animation temporary setups
69
71
animated = false ,
72
+ // Other rigibody props from parent
73
+ ...props
70
74
} : EcctrlProps ) {
71
75
const characterRef = useRef < RapierRigidBody > ( ) ;
72
76
const characterModelRef = useRef < THREE . Group > ( ) ;
@@ -260,6 +264,12 @@ export default function Ecctrl({
260
264
"Slope Ray" ,
261
265
{
262
266
showSlopeRayOrigin : false ,
267
+ slopeMaxAngle : {
268
+ value : slopeMaxAngle ,
269
+ min : 0 ,
270
+ max : 1.57 ,
271
+ step : 0.01
272
+ } ,
263
273
slopeRayOriginOffest : {
264
274
value : capsuleRadius ,
265
275
min : 0 ,
@@ -290,6 +300,7 @@ export default function Ecctrl({
290
300
) ;
291
301
// Apply debug values
292
302
showSlopeRayOrigin = slopeRayDebug . showSlopeRayOrigin ;
303
+ slopeMaxAngle = slopeRayDebug . slopeMaxAngle ;
293
304
slopeRayLength = slopeRayDebug . slopeRayLength ;
294
305
slopeRayDir = slopeRayDebug . slopeRayDir ;
295
306
slopeUpExtraForce = slopeRayDebug . slopeUpExtraForce ;
@@ -423,14 +434,14 @@ export default function Ecctrl({
423
434
/**
424
435
* Setup moving direction
425
436
*/
426
- // Only apply slope extra force when slope angle is between 0.2-1 , actualSlopeAngle < 1
437
+ // Only apply slope extra force when slope angle is between 0.2 and slopeMaxAngle , actualSlopeAngle < slopeMaxAngle
427
438
if (
428
- actualSlopeAngle < 1 &&
439
+ actualSlopeAngle < slopeMaxAngle &&
429
440
Math . abs ( slopeAngle ) > 0.2 &&
430
- Math . abs ( slopeAngle ) < 1
441
+ Math . abs ( slopeAngle ) < slopeMaxAngle
431
442
) {
432
443
movingDirection . set ( 0 , Math . sin ( slopeAngle ) , Math . cos ( slopeAngle ) ) ;
433
- } else if ( actualSlopeAngle >= 1 ) {
444
+ } else if ( actualSlopeAngle >= slopeMaxAngle ) {
434
445
movingDirection . set (
435
446
0 ,
436
447
Math . sin ( slopeAngle ) > 0 ? 0 : Math . sin ( slopeAngle ) ,
@@ -757,7 +768,7 @@ export default function Ecctrl({
757
768
// );
758
769
759
770
if ( rayHit && rayHit . toi < floatingDis + rayHitForgiveness ) {
760
- if ( slopeRayHit && actualSlopeAngle < 1 ) {
771
+ if ( slopeRayHit && actualSlopeAngle < slopeMaxAngle ) {
761
772
canJump = true ;
762
773
}
763
774
} else {
@@ -778,7 +789,7 @@ export default function Ecctrl({
778
789
const rayHitObjectBodyType = rayHit . collider . parent ( ) . bodyType ( ) ;
779
790
const rayHitObjectBodyMass = rayHit . collider . parent ( ) . mass ( ) ;
780
791
// Body type 0 is rigid body, body type 1 is fixed body, body type 2 is kinematic body
781
- // And iff it stands on big mass object (>0.5)
792
+ // And if it stands on big mass object (>0.5)
782
793
if (
783
794
( rayHitObjectBodyType === 0 || rayHitObjectBodyType === 2 ) &&
784
795
rayHitObjectBodyMass > 0.5
@@ -975,11 +986,11 @@ export default function Ecctrl({
975
986
return (
976
987
< RigidBody
977
988
colliders = { false }
978
- position = { [ 0 , 5 , 0 ] }
979
- friction = { - 0.5 }
980
- gravityScale = { 1.2 }
981
989
canSleep = { false }
982
990
ref = { characterRef }
991
+ position = { props . position || [ 0 , 5 , 0 ] }
992
+ friction = { props . friction || - 0.5 }
993
+ { ...props }
983
994
>
984
995
< CapsuleCollider args = { [ capsuleHalfHeight , capsuleRadius ] } />
985
996
< group ref = { characterModelRef } userData = { { camExcludeCollision : true } } >
@@ -1003,7 +1014,7 @@ export default function Ecctrl({
1003
1014
) ;
1004
1015
}
1005
1016
1006
- export type EcctrlProps = {
1017
+ export interface EcctrlProps extends RigidBodyProps {
1007
1018
children ?: ReactNode ;
1008
1019
debug ?: boolean ;
1009
1020
capsuleHalfHeight ?: number ;
@@ -1039,6 +1050,7 @@ export type EcctrlProps = {
1039
1050
dampingC ?: number ;
1040
1051
// Slope Ray setups
1041
1052
showSlopeRayOrigin ?: boolean ;
1053
+ slopeMaxAngle ?: number ;
1042
1054
slopeRayOriginOffest ?: number ;
1043
1055
slopeRayLength ?: number ;
1044
1056
slopeRayDir ?: { x : number ; y : number ; z : number } ;
@@ -1051,4 +1063,6 @@ export type EcctrlProps = {
1051
1063
autoBalanceDampingOnY ?: number ;
1052
1064
// Animation temporary setups
1053
1065
animated ?: boolean ;
1066
+ // Other rigibody props from parent
1067
+ props ?: RigidBodyProps ;
1054
1068
} ;
0 commit comments