isGrounded implementation for player

This commit is contained in:
azykov@mail.ru 2026-06-03 22:27:24 +03:00
parent 1011b7d3fc
commit d7b484e1be
No known key found for this signature in database
1 changed files with 5 additions and 2 deletions

View File

@ -30,6 +30,7 @@ export const CharacterView = observer(function ({ character, editMode }: Charact
const rbRef = useRef<RapierRigidBody>(null); const rbRef = useRef<RapierRigidBody>(null);
const [, get] = useKeyboardControls(); const [, get] = useKeyboardControls();
const { gl } = useThree(); const { gl } = useThree();
const groundContacts = useRef(0);
const yawRef = useRef(0); const yawRef = useRef(0);
const pitchRef = useRef(0); const pitchRef = useRef(0);
@ -93,10 +94,10 @@ export const CharacterView = observer(function ({ character, editMode }: Charact
const vz = (fwdZ * fwdScale + fwdX * rightScale) * SPEED; const vz = (fwdZ * fwdScale + fwdX * rightScale) * SPEED;
const cur = rbRef.current.linvel(); const cur = rbRef.current.linvel();
const isStanding = Math.abs(cur.y) < 0.5; const isGrounded = groundContacts.current > 0;
const vy = cur.y + const vy = cur.y +
(jump && !jumpPressedRef.current && isStanding (jump && !jumpPressedRef.current && isGrounded
? JUMP_SPEED ? JUMP_SPEED
: 0); : 0);
jumpPressedRef.current = jump; jumpPressedRef.current = jump;
@ -111,6 +112,8 @@ export const CharacterView = observer(function ({ character, editMode }: Charact
colliders="cuboid" colliders="cuboid"
lockRotations lockRotations
position={[pos[0] + 0.5, pos[1] + 0.5, pos[2] + 0.5]} position={[pos[0] + 0.5, pos[1] + 0.5, pos[2] + 0.5]}
onCollisionEnter={() => { groundContacts.current++; }}
onCollisionExit={() => { groundContacts.current--; }}
onSync={(data) => { onSync={(data) => {
// state.game?.setCharacterTransform( // state.game?.setCharacterTransform(
// { position: data.position, look: character.transform.look }, // { position: data.position, look: character.transform.look },