diff --git a/src/components/CharacterView.tsx b/src/components/CharacterView.tsx index 911d720..694a713 100644 --- a/src/components/CharacterView.tsx +++ b/src/components/CharacterView.tsx @@ -9,6 +9,7 @@ import { useKeyboardControls } from "@react-three/drei"; import type { RapierRigidBody } from "@react-three/rapier"; const SPEED = 5; +const JUMP_SPEED = 8; const SENSITIVITY = 0.002; // Shoulder offset in character-local space: right, up, behind const SHOULDER_OFFSET = new Vector3(0.1, 1.5, 2.5); @@ -33,10 +34,11 @@ export const CharacterView = observer(function ({ character, editMode }: Charact const yawRef = useRef(0); const pitchRef = useRef(0); const mouseRef = useRef({ x: 0, y: 0 }); + const jumpPressedRef = useRef(false); useEffect(() => { if (editMode) - return; + return; const canvas = gl.domElement; const onClick = () => canvas.requestPointerLock(); @@ -81,7 +83,7 @@ export const CharacterView = observer(function ({ character, editMode }: Charact // Movement relative to character facing direction if (!state.game?.isPaused && !editMode) { - const { forward, backward, left, right } = get(); + const { forward, backward, left, right, jump } = get(); const fwdX = -Math.sin(yaw); const fwdZ = -Math.cos(yaw); const fwdScale = (forward ? 1 : 0) - (backward ? 1 : 0); @@ -90,7 +92,16 @@ export const CharacterView = observer(function ({ character, editMode }: Charact const vx = (fwdX * fwdScale - fwdZ * rightScale) * SPEED; const vz = (fwdZ * fwdScale + fwdX * rightScale) * SPEED; const cur = rbRef.current.linvel(); - rbRef.current.setLinvel({ x: vx, y: cur.y, z: vz }, true); + + const isStanding = Math.abs(cur.y) < 0.5; + + const vy = cur.y + + (jump && !jumpPressedRef.current && isStanding + ? JUMP_SPEED + : 0); + jumpPressedRef.current = jump; + + rbRef.current.setLinvel({ x: vx, y: vy, z: vz }, true); } });