Search Unity

How to know whether is over mouse UI or not?

Discussion in '2D' started by Codoet, Sep 30, 2019.

  1. Codoet

    Codoet

    Joined:
    Jun 22, 2019
    Posts:
    16
    Hi,

    I want add to my 2d project 3d content as terrain but I have to rotate my UI and grid(for tilemaps) about 90 degrees. I'm doing raycasting and it's totaly ignoring object hit nor UI spawned over it.

    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    2. var origin = new Vector3(ray.origin.x, ray.origin.z, ray.origin.y);
    3. RaycastHit2D hit = Physics2D.Raycast(origin, ray.direction, Mathf.Infinity);
    This is happening as well after I spawned gameobject and I want to move it to another position https://imgur.com/a/g7HKfgE
     
    Last edited: Sep 30, 2019
  2. coidevoid

    coidevoid

    Joined:
    Oct 26, 2017
    Posts:
    55
    if (EventSystem.current.IsPointerOverGameObject())
     
  3. Codoet

    Codoet

    Joined:
    Jun 22, 2019
    Posts:
    16
    I have problem with offset which is including Unity(imgur in first post). Idk why but it happens. I checked positions of current extisting object and position of ray. They were almost same so the hit should be there but Raycast retuns false...

    So I cant catch hit nor raycast on the point but it's able to capture something from UI but in certain positions that can be different and here comes the magical offset.

    If I change the rotation to 0 (to default), it casts great.(Ofc with small changes)
     
    Last edited: Sep 30, 2019