pmndrs / ecctrl

🕹️ A floating rigibody character controller
MIT License
533 stars 63 forks source link

Drei Positional Audio compatibility #16

Closed jonathanalpmyr closed 5 months ago

jonathanalpmyr commented 11 months ago

Hey! I noticed that the component doesn't seem to work like its supposed to when using Ecctrl. The sound volume from the Positional Audio are the same no matter how far away the character/camera is.

ErdongChen-Andrew commented 11 months ago

Thanks for bringing this up @jontelol . I'll take a look later on.

ErdongChen-Andrew commented 11 months ago

I believe the default setting of the PositionalAudio listener is linked to the camera. However, since the ecctrl followcam's position is always at (0,0,0), you'll need to attach the listener to another mesh, such as your character model. Here's an example of adding the listener to a box character:

const soundRef = useRef()
const playerRef = useRef()
useEffect(()=>{
  playerRef.current.add(soundRef.current.listener)  
}, [])
...
// In this example audio is locate at (0,0,0)
<PositionalAudio url="/xxx.mp3" ref={soundRef} />
...
<Ecctrl>
  <mesh ref={playerRef} rotation={[0,Math.PI,0]}>
    <boxGeometry args={[1,1,1]}/> 
  </mesh>
</Ecctrl>

Hope this solve your problem 🙂