From d7b484e1be4c71d10af82f07b6f5578fe28a3d40 Mon Sep 17 00:00:00 2001 From: "azykov@mail.ru" Date: Wed, 3 Jun 2026 22:27:24 +0300 Subject: [PATCH] isGrounded implementation for player --- src/components/CharacterView.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/components/CharacterView.tsx b/src/components/CharacterView.tsx index 694a713..9d33ed2 100644 --- a/src/components/CharacterView.tsx +++ b/src/components/CharacterView.tsx @@ -30,6 +30,7 @@ export const CharacterView = observer(function ({ character, editMode }: Charact const rbRef = useRef(null); const [, get] = useKeyboardControls(); const { gl } = useThree(); + const groundContacts = useRef(0); const yawRef = 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 cur = rbRef.current.linvel(); - const isStanding = Math.abs(cur.y) < 0.5; + const isGrounded = groundContacts.current > 0; const vy = cur.y + - (jump && !jumpPressedRef.current && isStanding + (jump && !jumpPressedRef.current && isGrounded ? JUMP_SPEED : 0); jumpPressedRef.current = jump; @@ -111,6 +112,8 @@ export const CharacterView = observer(function ({ character, editMode }: Charact colliders="cuboid" lockRotations position={[pos[0] + 0.5, pos[1] + 0.5, pos[2] + 0.5]} + onCollisionEnter={() => { groundContacts.current++; }} + onCollisionExit={() => { groundContacts.current--; }} onSync={(data) => { // state.game?.setCharacterTransform( // { position: data.position, look: character.transform.look },