Search Unity

2D Screen Point Transform and CanvasScaler

Discussion in '2D' started by LittleCodingFox, Jul 7, 2015.

  1. LittleCodingFox

    LittleCodingFox

    Joined:
    Jul 2, 2015
    Posts:
    11
    I'm getting the screen position of something to show an object on top. However, because I'm using a Canvas Scaler, this doesn't always work because it doesn't take into account the canvas scaler. With it disabled, this works properly. How can I properly use the canvas scaler with this?


    Code (CSharp):
    1. Vector2 ActualPosition;
    2. RectTransform CanvasTransform = TheCanvas.transform as RectTransform;
    3.  
    4. RectTransformUtility.ScreenPointToLocalPointInRectangle(CanvasTransform, Position + new Vector2(0, CardOverlaySize.y / 2 + 10), TheCanvas.worldCamera, out ActualPosition);
    5.  
    6. Vector3 TransformedPosition = TheCanvas.transform.TransformPoint(ActualPosition);
    7.  
    8. CardOverlayTransform.transform.position = TransformedPosition;
    9.  
    10.  
     
    Last edited: Jul 8, 2015
  2. LittleCodingFox

    LittleCodingFox

    Joined:
    Jul 2, 2015
    Posts:
    11
unityunity