- Home /
Physics.Raycast passing through Colliders
I'm using the following code in Unity 2020.3.3f1 to detect clicked objects, but I noticed that spamming mouse button sometimes results in the Raycast just passing through a "solid" collider that it should detect.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f)){
// ...
}
I already checked online and here on Unity Answers, but none of the mentioned answers or suggestions help and it seems to happen randomly while spamming mouse clicks. There's no pattern to it as to why it might happen and all clicked colliders are exactly the same. None are on a wrong layer, too far away or anything like that.
The Ray just seemingly starts passing through a random Collider until I've clicked on a different one (which it detects), after which the Ray will detect the Collider that had issues previously.. :|
Only thing I can think of is that I'm changing semi-rapidly the position of those objects via transform.position = ... but visually the GameObjects that end up having issues seem to be correctly placed including the Collider (I think?)
So I don't think that the Collider should be "magically" missing or not have moved with the GameObject ?
Answer by Bunny83 · Apr 27, 2021 at 06:51 PM
It's not clear what you exactly mean by "passing through". A Raycast is not a ray that somehow travels through space :). It directly tests for intersections with colliders. One thing you just mentioned in the comment but not in your question is that you seem to be moving colliders around. Do those colliders have a kinematic rigidbody on them? If not you should not move them at all.
Static colliders must not be moved at all. Colliders that do not belong to a rigidbody (kinematic or non-kinematic) are considered to be part of the world and should never move, rotate or scale. Doing so can have quite a performance hit as the world collider is recreated each time. So any moving / rotating / changing collider should be part of a rigidbody. Kinematic rigidbodies are meant to be controlled by scripts. So it's "safe" to just move their transforms around.
Also keep in mind that modifying the Transform of a collider / rigidbody does not immediately affect the collider representation on the physics side. If you want to raycast immediately after a change you have to call Physics.SyncTransforms().
Thanks for pointing it out :)
Forgot to accept the answer earlier, but I guess that was the issue. Once I added a Kinematic Rigidbody the issue seemed to disappear. At least I couldn't reproduce it manually, though I didn't get around to write some tests to run a few times "just in case" hah