Raycasthit changes behavior after certain time
I am getting crazy. I have a raycast to detect the floor. During the first two minutes, it works fine, but after having the game running doing nothing for about two minutes, then the raycast starts having a weird behavior. This is the line:
if (Physics.Raycast(transform.position + Vector3.up * 0.5f, Vector3.down, out rayHit, 2.5f, GetCollisionLayer()))
I have the object in the vertical position 3.0, so it lauches a ray from the position 3.5 down, for 2.5 units. The floor has its contact point at 2.0f. And this works fine for the two first minutes. After that:
Physics.Raycast returns no hit. The character keeps falling. When the character's y position goes under 2.0, then it detects an obstacle with the collision point in 2.0f.
What the hell is going on? I assume it's some kind of optimization that is ruining the calculation. Nothing changes in the scene or the code to assume something happened.
I attach a picture
As you can see, the calculation goes to the else (not collision detected). But, as you can see, a Physics.RaycastAll to 5 meters brings a collider at 0.74 units of distance, while hits2 returns nothing, although the max distance is set to 2.5 units.