pmndrs / ecctrl

🕹️ A floating rigibody character controller
MIT License
465 stars 49 forks source link

Is there a way to access to object position data un useFrame? #43

Closed Dukeyeboah closed 2 months ago

Dukeyeboah commented 2 months ago

I love this library and it's working great for me with the project i'm building. However I want to be able to access my meshes position / translation information to know its coordinates. So that if it falls for example, I can reload t=it back to its initial positoin. However I can't seem to get access to this data, is there a way to get this ?

Where my mesh has I tried using body.current.position, in useFrame, but the vector coordinates of the position don't update when I move around. I also tried using body.current.translation() but there was an error

ErdongChen-Andrew commented 2 months ago

Thanks @Dukeyeboah , the translation() should be correct. Giving a ref, and you could log the position like so: if (body.current) console.log(body.current.translation())

Dukeyeboah commented 2 months ago

like so:

//SUN COMPONENT const Sun = () => {

const sunMaterialRef = useRef(); const sun = useRef();

useFrame((state, delta) => { const elapsedTime = state.clock.getElapsedTime();

if (sun.current) console.log(sun.current.translation())

});

return ( <> <mesh ref={sun} scale={[0.25, 0.25, 0.25]} geometry={sunGeometry}

</>

); };

export default Sun;

Is this appropriate? because i'm getting the following console error:

Sun.jsx:59 Uncaught TypeError: sun.current.translation is not a function at Object.current (Sun.jsx:59:46) at render$1 (chunk-YZGFH4DN.js?v=3142cbc5:17095:22) at loop (chunk-YZGFH4DN.js?v=3142cbc5:17116:19)

ErdongChen-Andrew commented 2 months ago

You need to assign the ref to <Ecctrl ref={sun} />

Dukeyeboah commented 2 months ago

Thank you that makes sense. It works.