I have a panel in my UI. It is actually a box, which is a subcomponent of a prefab, which I instantiate from script at a position in front of the camera (and under it hierarchically). The box has a BoxCollider component. I can see in the scene view that the collider matches the box itself. When the screen is tapped, I’m doing this:
var ray = Camera.main.ScreenPointToRay (screenPos);
RaycastHit hit;
print ("InfluenceEnvironment: check hit");
if (Physics.Raycast (ray, out hit, 500f)) // Note that the panel's distance from the camera is less than 500, and I have tried specifying no distance and thus casting an infinite ray, but the effect is identical.
{
print (string.Format("InfluenceEnvironment: Got hit: {0}", hit.transform.gameObject.name));
Debug.DrawRay(ray.origin, ray.direction*2000, Color.magenta, 10);
...
}
If I touch the middle of my panel, all is fine. But if I click the edge, it doesn’t work. I can see in the scene view that the ray from Debug.DrawRay() is passing through the collider in question. Yet the object in the RaycastHit is the terrain, which lies behind the box. And, bizarrely, if I change the collider at all in the inspector (e.g. changing x pos by 0.0001), it starts working (but making the equivalent change from code does not help). Simply disabling / re-enabling the collider also has this effect of fixing it. I’ve tried changing to a capsule collider instead, just to see it that works, but also have the same problem.
I also see that if I add the prefab to the scene manually instead of in script, that also works.
I wonder if the problem is related to the one discussed in http://forum.unity3d.com/threads/572…-objects/page2. However, I don’t think so, as it persists even when none of the objects involved nor the camera move.
I am using Unity 4.0.1f2
Any thoughts / workaround would be welcome! Regards,
Andy