- Home /
RaycastAll isn't finding all possible collisions
In my game you have the ability to flip board tiles, and I'm using a "fake" grid system to hold tile information. What I'm trying to do is detect that when a tile has been flipped what is now on the other side.
To do this I have a game object which represents the tile and holds 18 more game objects which represent the squares; 9 facing up and 9 facing down. The tile parent object is flipped using Transform.Rotate depending on whether it is flipped left to right, or top to bottom, and vice-versa. Once the "fake" grid has been flipped I raycast from each of the 9 grid positions.
Usually, this system works brilliant! The raycast finds all the hits and filters out the correct game object it is seeking. Unfortunately, occasionally, but frequently enough to ruin my game logic, it doesn't find the raycast hits from the flipped tile. I know the raycast is working, however it doesn't return the tile I'm looking for.
This code is called many times inside a deep-rooted coroutine because it's calculating some AI calculations. I have tried the route of waiting a frame after flipping the tile to raycast, I have also tried various colliders on the objects (currently they are box colliders which aren't triggers and do not have a rigidbody). However, I still have the same issue.. sometimes it works, sometimes it doesn't.
Is there a particular reason that Physics.RaycastAll does not return ALL of the collisions? Could flipping the game object sometimes prevent this from being detected?
I have included the flipping and raycasting code below, but have omitted my Debug checks. They just confirm when it isn't working, and what the raycast is finding.
// Flip the tile object
switch(flipDirection)
{
case TileController.Flip_Direction.Left:
case TileController.Flip_Direction.Right:
tileParent_Object.transform.Rotate(new Vector3(0.0f, 0.0f, 180.0f));
break;
case TileController.Flip_Direction.Up:
case TileController.Flip_Direction.Down:
tileParent_Object.transform.Rotate(new Vector3(180.0f, 0.0f, 0.0f));
break;
}
float yPosition = -2.5f;
float distance = 5.0f;
int layerMask = 1 << LayerMask.NameToLayer("Invisible");
// Raycast from the grid object to determine what is hit
for(int squareIndex = 0; squareIndex < tileGrid_Object.m_Squares.Count; squareIndex++)
{
// Find the position of the grid on the original board and adjust to accomadate ray
Vector3 grid_Square_Position = tileGrid_Object.m_Squares[squareIndex].transform.position;
grid_Square_Position.y = yPosition;
Ray square_Ray = new Ray(grid_Square_Position, Vector3.up);
RaycastHit[] hit_List = Physics.RaycastAll(square_Ray, distance * distance, layerMask);
//Debug.DrawRay(grid_Square_Position, Vector3.up, Color.white);
for(int hitIndex = 0; hitIndex < hit_List.Length; hitIndex++)
{
if(hit_List[hitIndex].collider.gameObject.tag == Tags.Clone_Square)
{
tileGrid_Object.m_Squares[squareIndex].GetComponent<Square>().m_Type = hit_List[hitIndex].collider.gameObject.GetComponent<Square>().m_Type;
tileGrid_Object.m_Squares[squareIndex].GetComponent<Square>().m_Exit = hit_List[hitIndex].collider.gameObject.GetComponent<Square>().m_Exit;
break;
}
}
}
You are using a two-sided collider like a box collider for your tiles? Have you done a Debug.DrawRay() of the right length and exa$$anonymous$$ed the geometry in the Scene view during a failure?
Yes, I have. The ray starts below the box collider. I have tested different distances as well, including the ray finishing inside and outside the collider.
If I don't rotate the tile object the raycast has no problem detecting the box colliders, however, after colliding it is less likely to detect the collision.
Answer by Wellmann · Aug 18, 2016 at 09:37 AM
This question is quiet old but if someone has a similar problem in the future: I've had a similar problem. What I found out is that the Rect Transform of the object has to be bigger than the Collider. Other wise the Collider will be trimmed by the Rect Transform bounds.