How to change the autorotation speed to the opposite via useEffect hook?

Greetings!

I need to reverse the autorotation speed when the azimuth angle reaches -1 or 1

I am new to this field, so I will be grateful for a detailed answer.

Thanks!

  const [speed, setSpeed] = useState(1); // 0.75

  const ref = useRef(null);

  useEffect(() => {
    if (ref.current !== null) {
      if (
        ref.current.getAzimuthalAngle() === -0.9 ||
        ref.current.getAzimuthalAngle() === 0.9
      )
        setSpeed(-speed);
    }
  }, [What should I pass here?]);

  <OrbitControls autoRotate ref={ref}/>
const ref = useRef()
useFrame(() => {
  const angle = ref.current.getAzimuthalAngle()
  ref.current.autoRotateSpeed = Math.abs(angle) >= 0.9 ? 0.5 : 1
})
return <OrbitControls autoRotate ref={ref} />
1 Like