working move kikle
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useRef } from "react";
|
import { useEffect, useRef } from "react";
|
||||||
import * as THREE from "three";
|
import * as THREE from "three";
|
||||||
import { useFrame } from "@react-three/fiber";
|
import { useFrame } from "@react-three/fiber";
|
||||||
import { InteractableObject } from "@/components/three/interaction/InteractableObject";
|
import { InteractableObject } from "@/components/three/interaction/InteractableObject";
|
||||||
@@ -40,22 +40,19 @@ export function Ebike({ position }: EbikeProps): React.JSX.Element {
|
|||||||
const model = useClonedObject(scene);
|
const model = useClonedObject(scene);
|
||||||
const movementMode = useGameStore((state) => state.player.movementMode);
|
const movementMode = useGameStore((state) => state.player.movementMode);
|
||||||
|
|
||||||
useFrame(() => {
|
useEffect(() => {
|
||||||
if (groupRef.current) {
|
(window as any).ebikeVisualGroup = groupRef;
|
||||||
if (movementMode === "ebike") {
|
return () => {
|
||||||
// Follow player physical position (capsule end)
|
(window as any).ebikeVisualGroup = null;
|
||||||
const playerPos = (window as any).playerPos || [0, 10, 0];
|
};
|
||||||
groupRef.current.position.set(playerPos[0], playerPos[1] - PLAYER_EYE_HEIGHT, playerPos[2]);
|
}, []);
|
||||||
|
|
||||||
// Match the e-bike's actual physical steering heading
|
useFrame(() => {
|
||||||
const angle = (window as any).ebikeAngle || 0;
|
if (groupRef.current && movementMode !== "ebike") {
|
||||||
groupRef.current.rotation.set(0, angle, 0);
|
|
||||||
} else {
|
|
||||||
// Reset to original position
|
// Reset to original position
|
||||||
groupRef.current.position.set(...position);
|
groupRef.current.position.set(...position);
|
||||||
groupRef.current.rotation.set(0, 0, 0);
|
groupRef.current.rotation.set(0, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const camPointPos: Vector3Tuple = [
|
const camPointPos: Vector3Tuple = [
|
||||||
|
|||||||
@@ -357,6 +357,17 @@ export function PlayerController({
|
|||||||
const yawRad = THREE.MathUtils.degToRad(EBIKE_CAMERA_TRANSFORM.rotation[1]) + ebikeAngle.current;
|
const yawRad = THREE.MathUtils.degToRad(EBIKE_CAMERA_TRANSFORM.rotation[1]) + ebikeAngle.current;
|
||||||
const rollRad = THREE.MathUtils.degToRad(EBIKE_CAMERA_TRANSFORM.rotation[2]);
|
const rollRad = THREE.MathUtils.degToRad(EBIKE_CAMERA_TRANSFORM.rotation[2]);
|
||||||
camera.rotation.set(pitchRad, yawRad, rollRad, "YXZ");
|
camera.rotation.set(pitchRad, yawRad, rollRad, "YXZ");
|
||||||
|
|
||||||
|
// Synchronize visual e-bike mesh position and rotation instantly to eliminate 1-frame follow latency jitter!
|
||||||
|
const ebikeVisual = (window as any).ebikeVisualGroup?.current;
|
||||||
|
if (ebikeVisual) {
|
||||||
|
ebikeVisual.position.set(
|
||||||
|
capsule.current.end.x,
|
||||||
|
capsule.current.end.y - PLAYER_EYE_HEIGHT,
|
||||||
|
capsule.current.end.z
|
||||||
|
);
|
||||||
|
ebikeVisual.rotation.set(0, ebikeAngle.current, 0);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
camera.position.copy(capsule.current.end);
|
camera.position.copy(capsule.current.end);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user