- Home /
Why does my Physics.Raycast not hit my BoxCollider?
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
Answer by Andy-Block · Feb 05, 2013 at 12:23 PM
I found a workaround for this. Immediately after creating my panel, I do:
textPanelObj.SetActive(false);
textPanelObj.SetActive(true);
Previously I had tried disabling / enabling the collider associated with the object, but not the object itself. This seems to resolve the problem.
Thank you for this. After changing a collider's size at runtime, a raycast wasn't hitting it until the next frame, which is unacceptable for my circumstances. Deactivating and reactivating it like this fixed the issue.
Answer by Qvintusdk · Jan 29, 2013 at 01:46 PM
Simply disabling / re-enabling the collider also has this effect of fixing it.
Isn't this your answer? If that fixes your problem why not grab the collider component and re-enable it upon instantiating it? It could simply be a bug with unity when instantiating object, that needs to be fixed and up untill then you need to use this workaround?
No, I don't think this helps. As I mentioned, the prefab is created from script, so I can't disable it in the editor, and although I didn't mention it, I've tried enabling / re-enabling from script already (just as I have tried adjusting the transform from script), and that doesn't help - it only works if you change it in the inspector when running in the editor. Thanks anyway for the suggestion.
you might be able to use the script from this question: http://answers.unity3d.com/questions/390571/odd-collision-behaviour.html
Hmm - interesting, sounds like it could be the same issue, indeed. I'm not in a state to test this right now, but will check it out when I can - thanks for the pointer!
I did get a chance to try this, but it didn't help, I'm afraid. Thanks for the suggestion, though! Anyway, I did find a workaround, which I'll add as an answer.
Answer by Owen-Reynolds · Jan 29, 2013 at 03:48 PM
If this is really a "UI box" which is being moved around to always occupy the same area on the screen, could just check screen coords directly, with no ray cast.
But, I'm wondering if the box is very close to the camera, and the rayCast is starting inside the box, so is ignoring it. ScreenPoint to ray starts NearPlane away from the camera (can print ray.origin
.) If that was the case, could adjust the ray to start further back: ray.origin -= ray.direction * 2;
Hmm - I don't think that's the problem; as I mentioned, Debug.DrawRay does show the ray going straight through the box. Also, if that was the issue, I would not have expected the disable / enable to have the effect of fixing it. Your point about just directly checking the screen coordinates is a good one, though. I'm not program$$anonymous$$g them directly in screen coordinates at the moment, but could work out a transform to allow me to check like that. However, I'd prefer to understand / work around the problem here differently, and rely on Unity to do the heavy lifting for me if possible. Thanks for your thoughts!
Answer by hubecube_ · Nov 12, 2015 at 12:14 PM
I am having the same issue in Unity 5.0.0f4
I have a moving platform that the player jumps on. The player object casts a ray to see if it has landed on something.
When the platform is placed in the scene, in the inspector there are no problem. The player lands and the raycast reports landing.
When the platform is instantiated through script the rays pass right through it.
Resetting (disable / enable) the game object or the collider did not help me.
I found that if I move the platform down 0.04f that it again begins to get hit by the raycasts
The solution to my problem was to add a script to the platform's Start() AND I had to constrain the y-axis movement on the rigidbody.
Vector3 posReset = this.transform.localPosition;
posReset.y -= 0.04f;
this.transform.localPosition = posReset;