I’m having trouble getting ray casts to work. In one situation, I successfully detect a collision and the associated game object. In the other situation, I apparently get NO collisions.
Casting a ray from an object “t” thinks there are no objects to hit, no matter how long I make the distance.
RaycastHit[] lefthits;
RaycastHit[] righthits;
// Find the origin by getting the collider associated with this component
Vector3 center = t.transform.GetComponent<MeshCollider>().bounds.center;
lefthits = Physics.RaycastAll(center, Vector3.left, 100.0f*tileSize);
righthits = Physics.RaycastAll(center, Vector3.right, 100.0f*tileSize);
// Neither array gets any hits
I’ve used debug.drawray to verify the rays. The origin and direction appear to be correct.
(I considered the possibility that there’s a margin of error that I cannot see in the scene view. But I take my “origin” point by grabbing the mesh collider’s center, so that should eliminate any error. I even tried a sphere cast and still got no collisions.)
On the other hand, when I cast a ray from the mouse, I get a collision just fine. So the collider is fully working. (Or at least partially working, assuming I didn’t miss some other important parameter.)
// This works just fine though
Ray r = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (r, out hit, Mathf.Infinity)) { Debug.Log ("Clicked on "+hit.transform.name); }
I can’t figure out what the heck I could be missing.