player jumps implemented

This commit is contained in:
azykov@mail.ru 2026-06-03 22:18:51 +03:00
parent da67932827
commit 1011b7d3fc
No known key found for this signature in database
1 changed files with 14 additions and 3 deletions

View File

@ -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,6 +34,7 @@ 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)
@ -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);
}
});