pmndrs / ecctrl

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

How to get position of current character #52

Closed Pear104 closed 2 months ago

Pear104 commented 2 months ago

Hi,

I'm a newbie.

I'm currently learning by doing some game experiment with react-three-fiber.

I want to get the current position of the character which is being controlled by the ecctrl, how can i get it

I tried a simple way by wrapping it inside a component and test it by logging the position but it still always the same position, although the character is moved:

const ControlledCharacter = () => {
  const charRef = useRef();
  window.onkeydown = (e) => {
    console.log(charRef.current);
    console.log(charRef.current.position); // always the same position, although the character is moved
  };
  return (
    <group ref={charRef}>
      <Ecctrl
        // debug
        animated
        floatHeight={0}
        followLight
        springK={2}
        dampingC={0.2}
        autoBalanceSpringK={1.2}
        autoBalanceDampingC={0.04}
        autoBalanceSpringOnY={0.7}
      >
        <EcctrlAnimation
          characterURL={characterURL}
          animationSet={animationSet}
        >
          <Demon position={[0, -0.65, 0]} />
        </EcctrlAnimation>
      </Ecctrl>
    </group>
  );
};

Am I doing something wrong? If so, is there any way to get the current position of the model? Thank you very much for your assistance and guidance.

ErdongChen-Andrew commented 2 months ago

You don't need the tag. Assign the chraRef to Ecctrl: <Ecctrl ref={charRef}> And this is the position of the character: charRef.current.translation()

Pear104 commented 2 months ago

You don't need the tag. Assign the chraRef to Ecctrl: <Ecctrl ref={charRef}> And this is the position of the character: charRef.current.translation()

Thank you for your guidance! Your suggestion worked perfectly. I really appreciate your help