Search Unity

Calculate correct position in Screen Space - Camera canvas

Discussion in 'Scripting' started by AzeExMachina, Nov 11, 2019.

  1. AzeExMachina

    AzeExMachina

    Joined:
    Jan 30, 2016
    Posts:
    14
    I'm in need of calculate the correct position of a 3D object which I'm displaying over a RawImage in my UI. If I put my UI image in the center of the screen I can calculate it perfectly, if I move my image around my UI canvas I'm getting results which are off by an offset, depending on the side I'm moving. I've taken a couple of screenshot of what I mean, the orange side is my 3D quad, the white square is just an image debugging where my point should be calculated.

    Correct:


    Wrong:


    The setup I have is this: - a world camera pointing on a 3D quad - a ui canvas with a dedicated perspective camera (Screen space - Camera) - a panel in my ui canvas displaying the 3D quad

    The code:
    Code (CSharp):
    1. var worldPoint = t.Value.MeshRenderer.bounds.min; //t.Value is the 3D quad
    2. var screenPoint = worldCamera.WorldToScreenPoint(worldPoint);
    3. screenPoint.z = (baseCanvas.transform.position - uiCamera.transform.position).magnitude; //baseCanvas is the UI canvas
    4. var pos = uiCamera.ScreenToWorldPoint(screenPoint);
    5. debugger.transform.position = pos; //debugger is just the square image used to see where my calculated point is landing
    Another test:
    Code (CSharp):
    1. var screenPoint = worldCamera.WorldToScreenPoint(t.Value.MeshRenderer.bounds.min);
    2. Vector2 localPoint;
    3. RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPoint, uiCamera, out localPoint); //rectTransform is my UI panel
    4. debugger.transform.localPosition = localPoint
    But I always get the same result, how can I make the correct calculation considering the offset?
     
  2. Vinayak-VC

    Vinayak-VC

    Joined:
    Apr 16, 2019
    Posts:
    60
    can you upload sample project?
     
  3. AzeExMachina

    AzeExMachina

    Joined:
    Jan 30, 2016
    Posts:
    14
    Not right now, I'll see if I can do something about it but can't promise anything
     
    Vinayak-VC likes this.