Hi all. I have encountered a weird raycast bug.
I have a very simple scene with only a couple of objects. One of these objects has a sphere collider on it, and this is the only collider in the scene.
I’m using a raycast to allow the player to click on the object and drag it around and it’s working correctly 99% of the time. But if I keep clicking on the object, after a random number of clicks the raycast stops working.
Once it stops, I can no longer drag the object around and it stays broken no matter how many times I click on it.
I am doing the raycast in FixedUpdate() (but have also tried normal Update and it makes no difference).
I have stepped through the code and the raycast is running but stops returning true.
I have put a Debug.DrawRay() in, and the Scene view is showing the collider in the correct place and that the ray passes through it. Yet it doesn’t detect the collider.
The number of clicks required to make this start happening is very random, I’ve had it occur after just 5 clicks, and not for hundreds of clicks, but I have reproduced this at least 10 times in my search for answers, I just keep clicking until it stops working.
Now here’s the weird bit that makes me think this is a Unity bug or weird undocumented interaction: Once it’s broken, if I just disable and re-enable the collider in the inspector, everything starts working again.
So my question is: Why would a collider stop being detected by a raycast? It almost feels to me like the object is “sleeping” or similar, but this is not a rigid body.
Here’s the raycast code:
private void FixedUpdate()
{
if(m_bMouseDown)
{
Vector3 v3Mouse = Input.mousePosition;
v3Mouse.z = Mathf.Abs(Camera.main.transform.position.z);
Ray ray = Camera.main.ScreenPointToRay(v3Mouse);
Debug.DrawLine(ray.origin, ray.origin + ray.direction * 10, Color.red, 5.0f);
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo)) //This if statement fails
{
m_Selected = hitInfo.collider.gameObject;
}
m_bMouseDown = false;
}
}