[SOLVED] Raycast distance issue

Hello,

I copied the code from Ray Casting | PlayCanvas Developer Site.

This says it can detect clicks on a particular object with a rigidbody. I have some cubes that move across the screen in my game using dt to update the position over time. They have kinematic rigidbodies since they are moved by my code. When they are close to the camera, the raycast works every time, but when they move further away, no clicks register.

I tried increasing ‘const to’ to be much larger than the camera far clip (which is still 1000) but that did not work. Why is the raycast not working at a distance?

Hi @robloxguy90 and welcome!

Are you be able to share a sample project with the problem so someone can take a look?

You can just share the editor link of the project.

I took a day break and returned and was able to solve it myself.

The issue was that I was raycasting to click position, which is not the center of the screen with a locked mouse. The mouse still moves independently outside of the screen.

I fixed this by just raycasting to the center of the screen instead. This problem was causing the illusion that raycasts weren’t going very far since when the object took up more screen space, it was more likely to have my mouse on it.

1 Like