Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Physics2D.Raycast is hitting the GameObject below the ray

Discussion in 'Physics' started by Renicon13377, Nov 19, 2023.

  1. Renicon13377

    Renicon13377

    Joined:
    Mar 1, 2021
    Posts:
    4
    Hello All,

    I am running into a weird raycast issue. Currently I have a grid and 1 script that when i left mouse click it creates a 2D raycast to check if there is already an existing GameObject with a collider where I clicked. If not then it gets the mouse world position > then gets the cell in that world position > then returns the center of that cell in world position > then instantiates a GameObject there. This GameObject is a small sprite with a 2D box collider which covers most of the sprite. The GameObjects do not spawn in a parent, and there are no other colliders in the project (this a fresh project).

    Whenever I try to spawn the GameObject to the left, right, or below an existing GameObject it works as intended, however, when i try to spawn a GameObject above an existing GameObject it collides with the one below it thus not spawning a GameObject. I have debugged the raycasts (red lines) to verify where the ray is going, it is clearly not hitting the GameObject below it. I have also debug.log the name of the GameObject it hits and it is always the GameObject directly below where I clicked.

    Any ideas???

    Code (CSharp):
    1. public class GetGridCellPosition : MonoBehaviour
    2. {
    3.     [SerializeField] private Camera _camera;
    4.     [SerializeField] private Grid _grid;
    5.     [SerializeField] private GameObject _gameObject;
    6.  
    7.     private Vector3 _mouseWorldPosition;
    8.     private int _testCounter = 0;
    9.  
    10.     private void Update()
    11.     {
    12.         if (Input.GetMouseButtonDown(0))
    13.         {
    14.             if (CheckIfClickedOnGameObject().collider != null)
    15.             {
    16.                 Debug.Log("Collided with something.");
    17.             }
    18.  
    19.             else
    20.             {
    21.                 Debug.Log("Collided with noting.");
    22.  
    23.                 PlaceObject();
    24.             }
    25.         }
    26.     }
    27.  
    28.     private RaycastHit2D CheckIfClickedOnGameObject()
    29.     {
    30.         _mouseWorldPosition = _camera.ScreenToWorldPoint(Input.mousePosition);
    31.  
    32.         Ray ray = new Ray(_mouseWorldPosition, Vector3.forward);
    33.  
    34.         RaycastHit2D hit = Physics2D.Raycast(_mouseWorldPosition, -Vector2.up, 13f);
    35.  
    36.         Debug.DrawRay(ray.origin, ray.direction * 10f, Color.red, 900f);
    37.  
    38.         if (hit.collider != null)
    39.         {
    40.             Debug.Log("Collided with: " +  hit.collider.name);
    41.         }
    42.  
    43.         return hit;
    44.     }
    45.  
    46.     private void PlaceObject()
    47.     {
    48.         Vector3Int _cellPosition = _grid.WorldToCell(_mouseWorldPosition);
    49.  
    50.         Vector3 _cellWorldPosition = _grid.GetCellCenterWorld(_cellPosition);
    51.  
    52.         GameObject _testObject = Instantiate(_gameObject, _cellWorldPosition, Quaternion.identity);
    53.  
    54.         _testCounter++;
    55.  
    56.         _testObject.name = "Test Object" + _testCounter.ToString();
    57.     }
    58. }
    Screenshot 2023-11-19 160511.png Screenshot 2023-11-19 161219.png
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    757
    You're understandably confused by the directions. The 2D raycast can't cast a ray towards the camera.

    Try this instead:
    Code (CSharp):
    1. Collider2D c=Physics2D.OverlapPoint(_mouseWorldPosition);
     
    MelvMay likes this.