Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

properly sync intersectObjects array in useFollowCam hook #118

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/hooks/useFollowCam.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useThree } from "@react-three/fiber";
// import { useRapier } from "@react-three/rapier";
import { useEffect, useMemo } from "react";
import { useEffect, useMemo, useState } from "react";
import * as THREE from "three";
import type { camListenerTargetType } from "../Ecctrl";

Expand All @@ -10,6 +10,7 @@ export const useFollowCam = function (props: UseFollowCamProps) {
const disableFollowCamPos = props.disableFollowCamPos;
const disableFollowCamTarget = props.disableFollowCamTarget;
// const { rapier, world } = useRapier();
const [render, setRender] = useState(false);

let isMouseDown = false;
let previousTouch1: Touch = null;
Expand Down Expand Up @@ -293,6 +294,23 @@ export const useFollowCam = function (props: UseFollowCamProps) {
};
});

useEffect(() => {
function onChildAdded() {
setRender((prev) => !prev);
}
function onChildRemoved() {
setRender((prev) => !prev);
}

scene.addEventListener("childadded", onChildAdded);
scene.addEventListener("childremoved", onChildRemoved);

return () => {
scene.removeEventListener("childadded", onChildAdded);
scene.removeEventListener("childremoved", onChildRemoved);
};
}, [scene]);

return { pivot, followCam, cameraCollisionDetect, joystickCamMove };
};

Expand Down