Skip to content

Commit f5a5afe

Browse files
feat: disable control
1 parent d4e86d0 commit f5a5afe

File tree

7 files changed

+44
-62
lines changed

7 files changed

+44
-62
lines changed

example/Experience.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export default function Experience() {
3131
/**
3232
* Debug settings
3333
*/
34-
const { physics, disableFollowCam } = useControls("World Settings", {
34+
const { physics, disableControl, disableFollowCam } = useControls("World Settings", {
3535
physics: false,
36+
disableControl: false,
3637
disableFollowCam: false,
3738
});
3839

@@ -80,6 +81,7 @@ export default function Experience() {
8081
autoBalanceDampingC={0.04}
8182
autoBalanceSpringOnY={0.7}
8283
autoBalanceDampingOnY={0.05}
84+
disableControl={disableControl}
8385
disableFollowCam={disableFollowCam}
8486
>
8587
{/* Replace your model here */}

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.88",
3+
"version": "1.0.89",
44
"author": "Erdong Chen",
55
"license": "MIT",
66
"description": "A floating rigibody character controller for R3F",

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ EcctrlProps: {
104104
floatHeight: 0.3, // Height of the character when floating
105105
characterInitDir: 0, // Character initial facing direction (in rad)
106106
followLight: false, // Enable follow light mode (name your light "followLight" before turn this on)
107+
disableControl: false, // Disable the ecctrl control feature
107108
disableFollowCam: false, // Disable follow camera feature
108109
disableFollowCamPos: { x: 0, y: 0, z: -5 }, // Camera position when the follow camera feature is disabled
109110
disableFollowCamTarget: { x: 0, y: 0, z: 0 }, // Camera lookAt target when the follow camera feature is disabled

src/Ecctrl.tsx

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
5454
floatHeight = 0.3,
5555
characterInitDir = 0, // in rad
5656
followLight = false,
57+
disableControl = false,
5758
disableFollowCam = false,
58-
disableFollowCamPos = { x: 0, y: 0, z: -5 },
59-
disableFollowCamTarget = { x: 0, y: 0, z: 0 },
59+
disableFollowCamPos = null,
60+
disableFollowCamTarget = null,
6061
// Follow camera setups
6162
camInitDis = -5,
6263
camMaxDis = -7,
@@ -134,14 +135,13 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
134135
let isModePointToMove: boolean = false
135136
let functionKeyDown: boolean = false
136137
let isModeFixedCamera: boolean = false
138+
let isModeCameraBased: boolean = false
137139
const setMoveToPoint = useGame((state) => state.setMoveToPoint)
138-
const setCameraBased = useGame((state) => state.setCameraBased);
139-
const getCameraBased = useGame((state) => state.getCameraBased);
140140
const findMode = (mode: string, modes: string) => modes.split(" ").some(m => m === mode)
141141
if (mode) {
142142
if (findMode("PointToMove", mode)) isModePointToMove = true
143143
if (findMode("FixedCamera", mode)) isModeFixedCamera = true
144-
if (findMode("CameraBasedMovement", mode)) setCameraBased(true)
144+
if (findMode("CameraBasedMovement", mode)) isModeCameraBased = true
145145
}
146146

147147
/**
@@ -775,7 +775,7 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
775775
bodyBalanceVecOnZ.set(bodyBalanceVec.x, bodyBalanceVec.y, 0)
776776

777777
// Check if is camera based movement
778-
if (getCameraBased().isCameraBased) {
778+
if (isModeCameraBased) {
779779
modelEuler.y = pivot.rotation.y
780780
pivot.getWorldDirection(modelFacingVec)
781781
// Update slopeRayOrigin to new positon
@@ -1023,6 +1023,32 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
10231023
dirLight.target = characterModelRef.current;
10241024
}
10251025

1026+
/**
1027+
* Camera movement
1028+
*/
1029+
pivotPosition.set(
1030+
currentPos.x + camTargetPos.x,
1031+
currentPos.y + (camTargetPos.y || (capsuleHalfHeight + capsuleRadius / 2)),
1032+
currentPos.z + camTargetPos.z
1033+
);
1034+
pivot.position.lerp(pivotPosition, 1 - Math.exp(-camFollowMult * delta));
1035+
1036+
if (!disableFollowCam) {
1037+
followCam.getWorldPosition(followCamPosition);
1038+
state.camera.position.lerp(followCamPosition, 1 - Math.exp(-camLerpMult * delta));
1039+
state.camera.lookAt(pivot.position);
1040+
}
1041+
1042+
/**
1043+
* Camera collision detect
1044+
*/
1045+
camCollision && cameraCollisionDetect(delta);
1046+
1047+
/**
1048+
* If disableControl is true, skip all following features
1049+
*/
1050+
if (disableControl) return;
1051+
10261052
/**
10271053
* Getting all gamepad control values
10281054
*/
@@ -1097,29 +1123,13 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
10971123

10981124
// If autobalance is off, rotate character model itself
10991125
if (!autoBalance) {
1100-
if (getCameraBased().isCameraBased) {
1126+
if (isModeCameraBased) {
11011127
characterModelRef.current.quaternion.copy(pivot.quaternion)
11021128
} else {
11031129
characterModelRef.current.quaternion.copy(characterModelIndicator.quaternion)
11041130
}
11051131
}
11061132

1107-
/**
1108-
* Camera movement
1109-
*/
1110-
pivotPosition.set(
1111-
currentPos.x + camTargetPos.x,
1112-
currentPos.y + (camTargetPos.y || (capsuleHalfHeight + capsuleRadius / 2)),
1113-
currentPos.z + camTargetPos.z
1114-
);
1115-
pivot.position.lerp(pivotPosition, 1 - Math.exp(-camFollowMult * delta));
1116-
1117-
if (!disableFollowCam) {
1118-
followCam.getWorldPosition(followCamPosition);
1119-
state.camera.position.lerp(followCamPosition, 1 - Math.exp(-camLerpMult * delta));
1120-
state.camera.lookAt(pivot.position);
1121-
}
1122-
11231133
/**
11241134
* Ray casting detect if on ground
11251135
*/
@@ -1381,11 +1391,6 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
13811391
*/
13821392
if (autoBalance && characterRef.current) autoBalanceCharacter();
13831393

1384-
/**
1385-
* Camera collision detect
1386-
*/
1387-
camCollision && cameraCollisionDetect(delta);
1388-
13891394
/**
13901395
* Point to move feature
13911396
*/
@@ -1504,6 +1509,7 @@ export interface EcctrlProps extends RigidBodyProps {
15041509
floatHeight?: number;
15051510
characterInitDir?: number;
15061511
followLight?: boolean;
1512+
disableControl?: boolean;
15071513
disableFollowCam?: boolean;
15081514
disableFollowCamPos?: { x: number, y: number, z: number };
15091515
disableFollowCamTarget?: { x: number, y: number, z: number };

src/hooks/useFollowCam.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,11 @@ export const useFollowCam = function (props: UseFollowCamProps) {
234234
followCam.rotation.x = camInitDir.x
235235
}, [])
236236

237-
// Set camera position to (0,0,0), if followCam is disabled set to disableFollowCamPos (default 0,0,-5)
237+
// If followCam is disabled set to disableFollowCamPos, target to disableFollowCamTarget
238238
useEffect(() => {
239239
if (disableFollowCam) {
240-
camera.position.set(disableFollowCamPos.x, disableFollowCamPos.y, disableFollowCamPos.z)
241-
camera.lookAt(new THREE.Vector3(disableFollowCamTarget.x, disableFollowCamTarget.y, disableFollowCamTarget.z))
242-
} else {
243-
camera.position.set(0, 0, 0)
240+
if (disableFollowCamPos) camera.position.set(disableFollowCamPos.x, disableFollowCamPos.y, disableFollowCamPos.z)
241+
if (disableFollowCamTarget) camera.lookAt(new THREE.Vector3(disableFollowCamTarget.x, disableFollowCamTarget.y, disableFollowCamTarget.z))
244242
}
245243
}, [disableFollowCam]);
246244

src/stores/useGame.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ export const useGame = /* @__PURE__ */ create(
1010
*/
1111
moveToPoint: null as THREE.Vector3,
1212

13-
/**
14-
* Check is camera based movement
15-
*/
16-
isCameraBased: false as boolean,
17-
1813
/**
1914
* Character animations state manegement
2015
*/
@@ -164,21 +159,6 @@ export const useGame = /* @__PURE__ */ create(
164159
moveToPoint: get().moveToPoint,
165160
};
166161
},
167-
168-
/**
169-
* Set/get camera based movement
170-
*/
171-
setCameraBased: (isCameraBased: boolean) => {
172-
set(() => {
173-
return { isCameraBased: isCameraBased };
174-
});
175-
},
176-
177-
getCameraBased: () => {
178-
return {
179-
isCameraBased: get().isCameraBased,
180-
};
181-
},
182162
};
183163
})
184164
);
@@ -200,7 +180,6 @@ export type AnimationSet = {
200180

201181
type State = {
202182
moveToPoint: THREE.Vector3;
203-
isCameraBased: boolean;
204183
curAnimation: string;
205184
animationSet: AnimationSet;
206185
initializeAnimationSet: (animationSet: AnimationSet) => void;
@@ -209,10 +188,6 @@ type State = {
209188
getMoveToPoint: () => {
210189
moveToPoint: THREE.Vector3;
211190
}
212-
setCameraBased: (isCameraBased: boolean) => void;
213-
getCameraBased: () => {
214-
isCameraBased: boolean;
215-
}
216191
} & {
217192
[key in keyof AnimationSet]: () => void;
218193
};

0 commit comments

Comments
 (0)