Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Convert from Screen Space - Camera (Canvas) to LineRenderer/World Space

Discussion in 'Editor & General Support' started by alexfirecheetah, Jun 10, 2023.

  1. alexfirecheetah

    alexfirecheetah

    Joined:
    Jul 8, 2020
    Posts:
    10
    So I have a Canvas (Screen Space - Camera) and I want to be able to add LineRenderer points from coordinates from that canvas. I have tried using:
    Code (CSharp):
    1. camera.ScreenToWorldPoint();
    ...but that doesn't seem to work. Here is what happens:

    I have a working area which is a rectangle that is 545.324 x 419.48 (Canvas Space).

    For testing purposes, I tried rendering a point at (0,0), which seemed to work. However, when I rendered a point at my rectangle's coordinates, it did not spawn there.

    Here is my testing code:
    Code (CSharp):
    1.  
    2. var testPoint = Instantiate(point);
    3.         float width = workingArea.rect.width;
    4.         float height = workingArea.rect.height;
    5.         Vector3 pos = camera.ScreenToWorldPoint(new Vector3(width, height, 10));
    6.         Debug.Log("Width: " + width);
    7.         Debug.Log("Height: " + height);
    8.         Debug.Log("x: " + pos.x + ", y: " + pos.y + ", z: " + pos.z);
    9.         testPoint.transform.position = pos;
    10.  
    When I Debug.Log() width and height, I get 545.324 & 419.48 (which are exactly right). The other Debug.log() for the xyz looks like this: x: -0.4921396, y: 1.95655, z: 0. These coordinates seem to be incorrect, because the point is not spawning at the top right hand corner like it is supposed to.

    Here is an image:
    upload_2023-6-10_8-45-37.png

    The red dot is where it is supposed to spawn. The black dot is my point (aka. where it does spawn).

    But when I replace width and height with (0,0), the point spawns at the bottom left hand corner of the working area, exactly where I want it to.

    Does anyone know why my black point doesn't spawn at the top right hand corner of the work area? Or how I can make that happen? And what makes the point spawn where it is spawning?

    (The black point is a sprite (not a canvas image) so it takes up world space. I am using this for testing because (if I am not incorrect) the line renderer also uses the same world space).

    Thank you for any help!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    What is
    workingArea
    ? Is that a
    RectTransform
    ?

    If so, that .rect is not screen coordinates. It's complicated, go check the docs for details.

    Most likely you can get to screen coordinates from where you are using RectTransformUtility

    Alternately you would need the Canvas, the Canvas render mode and the scaling mode to do the transform yourself.
     
  3. alexfirecheetah

    alexfirecheetah

    Joined:
    Jul 8, 2020
    Posts:
    10
    Yes, this was the issue. Thank you. For anyone who may need it, I got what I needed with:
    Code (CSharp):
    1.  
    2. Vector3[] corners = new Vector3[4];
    3. workingArea.GetWorldCorners(corners);
    4.  
    ...and from there I just calculated min and max x & y.
     
    Kurt-Dekker likes this.