Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted Physics.CheckSphere does not detect any objects on the layer.

Discussion in 'Scripting' started by a4chteam, Apr 22, 2021.

  1. a4chteam

    a4chteam

    Joined:
    Dec 22, 2020
    Posts:
    16
    Code (CSharp):
    1. public Vector2 GridSize;
    2.     public float CellSize;
    3.     public LayerMask CollisionLayer;
    4.  
    5.     Vector2 ZeroPosition;
    6.  
    7.     void Start()
    8.     {
    9.         ZeroPosition = new Vector2((transform.position.x - GridSize.x), (transform.position.x - GridSize.y));
    10.     }
    11.  
    12.     void OnDrawGizmos()
    13.     {
    14.         Gizmos.color = Color.green;
    15.         Gizmos.DrawWireCube(transform.position, new Vector3(GridSize.x, GridSize.y, 0));
    16.         Gizmos.color = Color.red;
    17.         for (float i = 0; i < GridSize.x; i+=0.75f)
    18.         {
    19.             for (float j = 0; j < GridSize.y; j+=0.75f)
    20.             {
    21.                 if(Physics.CheckSphere(new Vector3((ZeroPosition.x + 0.375f) + i, (ZeroPosition.y - 0.375f) - j, 0), CellSize, CollisionLayer))
    22.                 {
    23.                     Gizmos.DrawWireCube(new Vector3((ZeroPosition.x + 0.375f) + i, (ZeroPosition.y - 0.375f) - j, 0), new Vector3(CellSize, CellSize, 0));
    24.                 }
    25.             }
    26.         }
    27.     }
    The layer "CollisionLayer" has objects in it, but the Physics.CheckSphere does not detect them. What is the problem? (Box Collider is placed on objects)
     
  2. a4chteam

    a4chteam

    Joined:
    Dec 22, 2020
    Posts:
    16
    Ok, I found a solution. Use when facing a problem.

    Code (CSharp):
    1.     public Vector2 GridSize;
    2.     public float CellSize;
    3.     public LayerMask CollisionLayer;
    4.  
    5.     Vector2 ZeroPosition;
    6.  
    7.     void Start()
    8.     {
    9.         ZeroPosition = new Vector2((transform.position.x - GridSize.x), (transform.position.x - GridSize.y));
    10.     }
    11.  
    12.     void OnDrawGizmos()
    13.     {
    14.         Gizmos.color = Color.green;
    15.         Gizmos.DrawWireCube(transform.position, new Vector3(GridSize.x, GridSize.y, 0));
    16.         Gizmos.color = Color.red;
    17.         for (float i = 0; i < GridSize.x; i+=0.75f)
    18.         {
    19.             for (float j = 0; j < GridSize.y; j+=0.75f)
    20.             {
    21.                 RaycastHit2D Hit;
    22.                 Hit = Physics2D.Raycast(new Vector2((ZeroPosition.x + 0.375f) + i, (ZeroPosition.y - 0.375f) - j), transform.up, 0.2f, CollisionLayer);
    23.                 if (Hit.collider != null)
    24.                 {
    25.                     Gizmos.DrawWireCube(new Vector3((ZeroPosition.x + 0.375f) + i, (ZeroPosition.y - 0.375f) - j, 0), new Vector3(CellSize, CellSize, 0));
    26.                 }
    27.             }
    28.         }
    29.     }
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,643
    Your original problem was using Physics.CheckSphere instead of Physics2D.OverlapCircle. The two physics engines are completely separate.
     
unityunity