pmndrs / ecctrl

🕹️ A floating rigibody character controller
MIT License
467 stars 50 forks source link

Proposal to add a EcctrlProp to toggle camera follow #30

Closed valentincognito closed 5 months ago

valentincognito commented 5 months ago

Ecctrl is taking the default camera using const { scene, camera } = useThree(); This means if we add another camera and mark it as the default camera Ecctrl will immediately take control of the camera. But there are a use case where you want to cut to a different camera (let's say a flying map overview) and go back to the player's camera. So basically you want your new camera not to be controlled by ecctrl.

Therefore I would like to introduce a prop like camFollow: boolean then in Ecctrl.tsx line 989 we could do something like this:

/**
     *  Camera movement
     */
    if(camFollow) {
      pivotPosition.set(
        currentPos.x + camTargetPos.x,
        currentPos.y + (camTargetPos.y || (capsuleHalfHeight + capsuleRadius / 2)),
        currentPos.z + camTargetPos.z
      );
      pivot.position.lerp(pivotPosition, 1 - Math.exp(-camFollowMult * delta));
      state.camera.lookAt(pivot.position);
    }

and in useFollowCam.tsx we skip the mouse / touch events:

// Mouse move event
const onDocumentMouseMove = (e: MouseEvent) => {
    if(!camFollow) return false;
    ...

 // Mouse scroll event
  const onDocumentMouseWheel = (e: Event) => {
    if(!camFollow) return false;   

    ...

// Touch move event
  const onTouchMove = (e: TouchEvent) => {
    // prevent swipe to navigate gesture
    e.preventDefault();
    e.stopImmediatePropagation();

    if(!camFollow) return false;

That would help us cutting to different camera view when needed.

If you think it's a good idea I can create a pull request!

ErdongChen-Andrew commented 5 months ago

Thank you, @valentincognito ! That's a really good point! Actually, I'm already working on this feature and it will be released in the next update!

valentincognito commented 5 months ago

Awesome news! I'm closing this issue then. Thanks for your work :)

ErdongChen-Andrew commented 5 months ago

@valentincognito , you can now disable the followCam in v1.0.62: <Ecctrl disableFollowCam>