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

Raycast - API for dynamic updates props "from" and "to" #329

Open
Russo-creation opened this issue Jan 13, 2022 · 0 comments
Open

Raycast - API for dynamic updates props "from" and "to" #329

Russo-creation opened this issue Jan 13, 2022 · 0 comments

Comments

@Russo-creation
Copy link

Hi,

I wanted to attach raycast to the character to check if it is touching the ground before jump. I noticed that there is no option to change start and end points of raycast (like it is in the useSphere hook we can call an api and change position etc.) without causing the rerender the component which in my opinion can affect on performance and it is not comfortable to use.

For now a walkaround to not cause the rerender more than raycaster component is create separate component and change it state through the store.

Below part of code which shows a little the issue

const Raycaster = React.forwardRef((props, ref) => {

// get state from zustand with rerender component and apply it to "from" and "to" props

  const raycast = useRaycastClosest(
    {
      from: [0, 10, 0],
      to: [0, -10, 0],
      collisionFilterMask: 1,
      skipBackfaces: true,
    },
    ref
  );

  return <></>;
});

const Player = (props)=> {
  const refRaycast = React.createRef();

  const [ref, api] = useSphere(() => ({
    mass: 1,
    type: 'Dynamic',
    position: [-5, -8.5, 0],
    fixedRotation: true,
    allowSleep: false,
    collisionFilterGroup: 2,
    material: {
      friction: 0,
    },
    ...props,
  }));

  const velocity = useRef([0, 0, 0]);
  useEffect(() => api.velocity.subscribe((v) => (velocity.current = v)), []);

// useFrame in which check position of sphere and change the state through zustand
  
  return (
   <group>
     <Raycaster ref={refRaycast} />
       <mesh ref={ref}>
          <sphereBufferGeometry attach="geometry" args={[1, 8, 8]} />
          <meshStandardMaterial color="#a81b00" />
        </mesh>
   </group>
  )
}
@Russo-creation Russo-creation changed the title Raycast - change props "from" and "to" dynamically without rerendering component Raycast - API for dynamically updating props "from" and "to" Apr 25, 2022
@Russo-creation Russo-creation changed the title Raycast - API for dynamically updating props "from" and "to" Raycast - API for dynamic updates props "from" and "to" Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant