- Home /
Raycasting stops working randomly
Hello,
We have an issue in a game we just released: After a random time and under random circumstances our main raycasting stops getting any hits. Therefore players cannot interact with objects and are forced to reload the game. It's 40% of all the bug reports we get.
No one has found a way to reproduce it and we only caught it once in the editor. The ray is correct, the collider is there (it blocks movement), but raycast is getting zero hits. In the situation caught in the editor the only thing that fixed the issue was removing Rigidbody and adding it again. It doesn't seem to affect objects without a rigidbody.
We are using Unity 2019.4.19f1 LTS and the method Physics.RaycastAll (2 units distance, 3 layers in layermask). "Remove all rigidbodies and restore them" at some interval doesn't seem like a sensible option. Any ideas how to approach it?
Answer by FreeMindSA · Sep 13, 2021 at 10:41 AM
To anyone finding this. We figured it out. Several physical (non-kinematic rigidbody) objects in the scene were very far away from world origin without us knowing about it. Their position was far beyond float precision and gave warnings in the inspector. Deleting, deactivating or bringing those objects back to a more sensible position fixed the raycasting for us. It works everytime, so we had to add a system keeping physical objects' position in check.
Thanks for sharing. We have the same problem in 2020 and in 2021.
Answer by alexhapki · Feb 26 at 10:19 AM
Hi, @FreeMindSA I have got a similar issue. A solution I got was to turn "kinematic" on the rigidbody. It may not be a suitable solution for all projects, but it is one. Just wanted to let you know it.
Your answer
Follow this Question
Related Questions
Physics.IgnoreRaycastLayer putting GameObject on layer 4 water. 0 Answers
RaycastHit if/else Problem (bug?) 0 Answers
Physics.Raycasy not really using Layermask? 1 Answer
SphereCast Arguments 1 Answer