player jumps implemented
This commit is contained in:
parent
da67932827
commit
1011b7d3fc
|
|
@ -9,6 +9,7 @@ import { useKeyboardControls } from "@react-three/drei";
|
||||||
import type { RapierRigidBody } from "@react-three/rapier";
|
import type { RapierRigidBody } from "@react-three/rapier";
|
||||||
|
|
||||||
const SPEED = 5;
|
const SPEED = 5;
|
||||||
|
const JUMP_SPEED = 8;
|
||||||
const SENSITIVITY = 0.002;
|
const SENSITIVITY = 0.002;
|
||||||
// Shoulder offset in character-local space: right, up, behind
|
// Shoulder offset in character-local space: right, up, behind
|
||||||
const SHOULDER_OFFSET = new Vector3(0.1, 1.5, 2.5);
|
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 yawRef = useRef(0);
|
||||||
const pitchRef = useRef(0);
|
const pitchRef = useRef(0);
|
||||||
const mouseRef = useRef({ x: 0, y: 0 });
|
const mouseRef = useRef({ x: 0, y: 0 });
|
||||||
|
const jumpPressedRef = useRef(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (editMode)
|
if (editMode)
|
||||||
|
|
@ -81,7 +83,7 @@ export const CharacterView = observer(function ({ character, editMode }: Charact
|
||||||
|
|
||||||
// Movement relative to character facing direction
|
// Movement relative to character facing direction
|
||||||
if (!state.game?.isPaused && !editMode) {
|
if (!state.game?.isPaused && !editMode) {
|
||||||
const { forward, backward, left, right } = get();
|
const { forward, backward, left, right, jump } = get();
|
||||||
const fwdX = -Math.sin(yaw);
|
const fwdX = -Math.sin(yaw);
|
||||||
const fwdZ = -Math.cos(yaw);
|
const fwdZ = -Math.cos(yaw);
|
||||||
const fwdScale = (forward ? 1 : 0) - (backward ? 1 : 0);
|
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 vx = (fwdX * fwdScale - fwdZ * rightScale) * SPEED;
|
||||||
const vz = (fwdZ * fwdScale + fwdX * rightScale) * SPEED;
|
const vz = (fwdZ * fwdScale + fwdX * rightScale) * SPEED;
|
||||||
const cur = rbRef.current.linvel();
|
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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue