feat: add footsteps
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -54,10 +54,32 @@ export default function Player() {
|
||||
const confirmedSegment = useRef<number>(0);
|
||||
const hasTriggeredThisSegment = useRef<boolean>(false);
|
||||
|
||||
const footstepAudio = useRef<HTMLAudioElement[]>([]);
|
||||
const hasStepped = useRef<boolean>(false);
|
||||
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
playerRoot.set(camera.position.x, FEAR_SETTINGS.PLAYER_HEIGHT, camera.position.z);
|
||||
footstepAudio.current = Array.from({ length: 6 }, (_, i) => {
|
||||
const audio = new Audio(`fear/snd/footstep${i + 1}.mp3`);
|
||||
audio.volume = 0.4;
|
||||
return audio;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const playRandomFootstep = () => {
|
||||
if (footstepAudio.current.length === 0) return;
|
||||
|
||||
const randomIndex = Math.floor(Math.random() * footstepAudio.current.length);
|
||||
const audio = footstepAudio.current[randomIndex];
|
||||
|
||||
audio.currentTime = 0;
|
||||
audio.play().catch((err) => {
|
||||
console.warn("Footstep playback blocked by browser autocomplete/interaction rules.", err);
|
||||
});
|
||||
};
|
||||
|
||||
useFrame((state, delta) => {
|
||||
const dt = Math.min(delta, 0.1);
|
||||
|
||||
@@ -92,9 +114,19 @@ export default function Player() {
|
||||
if (isMoving)
|
||||
movementCounter.current += dt * 12;
|
||||
|
||||
const moveBobY = Math.sin(movementCounter.current) * 0.06 * bobIntensity.current;
|
||||
const sinWave = Math.sin(movementCounter.current);
|
||||
const moveBobY = sinWave * 0.06 * bobIntensity.current;
|
||||
const moveBobX = Math.cos(movementCounter.current / 2) * 0.04 * bobIntensity.current;
|
||||
|
||||
if (isMoving && sinWave < -0.9) {
|
||||
if (!hasStepped.current) {
|
||||
playRandomFootstep();
|
||||
hasStepped.current = true;
|
||||
}
|
||||
} else if (sinWave > 0) {
|
||||
hasStepped.current = false;
|
||||
}
|
||||
|
||||
const breatheTime = state.clock.elapsedTime * 1.8;
|
||||
const breatheBobY = Math.sin(breatheTime) * 0.03 * (1 - bobIntensity.current * 0.5);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user