RayCast2D ignor hitting own Collider

Viewed 424

Board.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Board : MonoBehaviour
{
    public Transform m_emptySprite;
    private int m_height = 14;
    private int m_width = 6;

    // number of rows where we won't have grid lines at the top
    public int m_header = 8;

    // Start is called before the first frame update
    void Start()
    {
        DrawEmptyCells();
    }

    void DrawEmptyCells()
    {
        for (int y = 0; y < m_height - m_header; y++)
        {
            for (int x = 0; x < m_width; x++)
            {
                Transform tile;
                tile = Instantiate(m_emptySprite, new Vector3(x, y, 0), Quaternion.identity) as Transform;
                tile.name = "Tile ( x = " + x.ToString() + " ,y = " + y.ToString() + ")";
                tile.transform.parent = transform;
            }
        }
    }
}

Tile.cs

public class Tile : MonoBehaviour
{
    private Vector2[] adjacentDirections = new Vector2[] { Vector2.up, Vector2.down, Vector2.left, Vector2.right };
  
    void OnMouseDown() 
    {
       GetAllAdjacentTiles();
    }

    private GameObject GetAdjacent(Vector2 castDir)
    {
        RaycastHit2D hit = Physics2D.Raycast(transform.position, castDir);
        if (hit.collider != null) 
        {
            print(hit.collider.gameObject.name);
            return hit.collider.gameObject;
        }
        return null;
    }

    private List<GameObject> GetAllAdjacentTiles()
    {
        List<GameObject> adjacentTiles = new List<GameObject>();
        for (int i = 0; i < adjacentDirections.Length; i++)
        {
            adjacentTiles.Add(GetAdjacent(adjacentDirections[i]));
        }
        return adjacentTiles;
    }
}

I have tried to use the code above to detect tiles in all 4 directions but when I click on a tile I just get the name of the tile that was clicked.

Each tile has the Tile Script and a BoxCollider2D attached, why is it not printing all 4 tiles surrounding the current tile?

1 Answers

By default for Physics2D you are hitting your own collider the Raycast starts in.

To solve this go to the Physics2D Settings via Edit → Project Settings → Physics2D and disable the option

Queries Start In Colliders

Enable this option if you want physics queries that start inside a Collider 2D to detect the collider they start in.

enter image description here

Related