UI line renderer is not drawing Line at mouse position!

Issue #183 resolved
Former user created an issue

I'm using UI line renderer and adding points through mouse position at run time but the issue is mouse position and line position is not at same position!!

Comments (7)

  1. Argonne DroidM

    The same happens to me, is there some settings we can check to avoid this problem? In my case the offset between drawn line and mouse position is smaller but still it is not working as expected

  2. Simon Jackson

    Welcome to the world of Anchors and Pivots. Something I'm challenged with each time I try to use screenspace coordinates. Once you have transformed the click point through the camera, you also have to allow for the current Pivot position. To make it easier, set the Pivot to 0,0.

    You should also try without the Canvas Scalar in case that is also having a "dragging" effect on the mouse.

    Check the Bezier editor, as I did a ton of work there working with ScreenToWorldPoint (which was a huge pain)

  3. Channabasappa Konin

    Hi Simon, can you please elaborate the steps to follow because I’m still facing this issue. Thank you.

  4. Simon Jackson

    I’ll see if I can add a function to allow creating points based on screen position to make it easier.

  5. Antonio Di Bacco

    Dear all,

    let me add some more information about a similar problem I’m facing:

    1. I have Pivot set to 0,0 for my UILineRenderer
    2. Using Camera.main.ViewportToWorldPoint(new Vector3(1,1,Camera.main.nearClipPlane)) I know that my camera goes from -4.4 to 4.4 on X axis, and -9.6 to 9.6 on Y axis in world units. My Canvas has exactly the same size. (Checked getting the world corners of canvas.GetComponent<RectTransform>())
    3. My phone has 1080 x 2340 pixels screen
    4. I expect that if I draw like this

    pointsListTrace.Add (new Vector3(-4.4f, -9.6f, 0));

    pointsListTrace.Add (new Vector3(4.4f, -9.6f, 0));

    pointsListTrace.Add (new Vector3(4.4f, 9.6f, 0));

    pointsListTrace.Add (new Vector3(-4.4f, 9.6f, 0));

    pointsListTrace.Add (new Vector3(-4.4f, -9.6f, 0));

    segmentsTrace [segmentTrace] = pointsListTrace.ToArray ();

    lineRendererTrace.Segments = segmentsTrace;

    I should get a rectangle on the border of my phone screen.

    Instead, the rectangle is smaller and even when I convert the Input.mousePosition into world coordinates there is an offset with the drawn trace, this offset is zero around the screen center but increases when I move far from the center.

    Here it is the UILineRenderer

    If I test on a phone with 1080x1920 resolution then the rectangle is exactly on the border of the phone.

  6. Log in to comment