Search Unity

How to find the X,Y on the object a ray cast hit in world space canvas?

Discussion in 'Physics' started by MSLTD, Oct 19, 2021.

  1. MSLTD

    MSLTD

    Joined:
    Mar 5, 2021
    Posts:
    4
    I have a world space canvas with a Image on it.

    When i click on it, i need to get the X,Y relative to the image.

    So if i clicked on its top left i would expect something near 0,0.... Bottom Right 800,600

    I've tried Screen Position & World Position, but there doesnt seem to be an object position.
    The object definitly hits.... but i need to know where it hit.

    Code (CSharp):
    1. PointerEventData pointer = new PointerEventData(EventSystem.current);
    2.         pointer.position = Input.mousePosition;
    3.  
    4.         var raycastResults = new List<RaycastResult>();
    5.         EventSystem.current.RaycastAll(pointer, raycastResults);
    6.  
    7.         foreach (var raycastHit in raycastResults)
    8.         {
    9.             Debug.Log($"Hit: {raycastHit.gameObject.name} Screen: {raycastHit.screenPosition}, World {raycastHit.worldPosition}");
    10.         }
    I'm sure its something simple i just cant seem to find it? Can someone point me in the right direction?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,458
    Please note that this is UI and not physics (the physics teams don't implement this stuff). It's UI/Event casting; can use physics or graphics. You're better off posting in the UI forum here.

    Someone might answer here but it's not related to anything in physics and all about Canvas/UI.
     
    MSLTD likes this.
  3. MSLTD

    MSLTD

    Joined:
    Mar 5, 2021
    Posts:
    4
    MelvMay likes this.