Get screenpoint of lineRenderer point with isometric camera

Hello everyone,
I’m trying to show a tooltip over a graph when the player hover a lineRenderer (that represents a graph).
I got my line renderer showing perfectly fine after player around a bit with local/world positions, and now I’m trying to show a tooltip asset right above the closest point to the cursor. to do that I’m comparing the Input.UI.Point (the default “Point” control when you add the input system) .GetValue() with the screenpoint poisition of the point composing the line renderer.

My problem is, I think my script to find the screenpoint coordinates of my lineRederer poisitions if off in a strange way.


I added some trace and my cursor is placed on the closest point , according to my script, to the end of the line (tip of the blue line on the top right, nothing is hidden or masked here).
As you can see it is visibly off.
Here is my code

  var pointLocal = graphLine.Line.GetPosition(i);
                    var pointWorld = graphLine.GameObject.transform.TransformPoint(pointLocal);
                    var pointScreen3 = camera.WorldToScreenPoint(pointWorld);
                    var pointScreen = new Vector2(pointScreen3.x, pointScreen3.y);
                    var pointDistance = Vector2.Distance(pointScreen, pointerOnScreen);

I should specify that the camera used is an orthographic camera, not the main camera, used to super-impose the UI/HUD. it’s a child of the main camera if that matters. It’s configured like this :

and the canvas that contains the line renderers and all the stuff is configured like that :

I must precise that in my script, I get the 1 from the canva’s “plane distance” as Z axis.
I’m ignoring it in my script as I don’t know what to do with it but I feel this is at the hearth of my troubles.
The thing is, I don’t know how to compare a Point Vector2 (flat space on screen) to a Vector3 with some depth, projected on the orthographic camera.

I suspect the problem is you are taking screen positions directly into your UI object.

That only works under certain precise states of canvas render and canvas scaler.

However, you can use this method to make it work in whatever setup you have:

https://docs.unity3d.com/ScriptReference/RectTransformUtility.ScreenPointToLocalPointInRectangle.html

It’s a bit fiddly… I suggest doing some experiments in a blank screen until you confirm you got it nailed down.

1 Like

Hi,
thanks for the tip.
I tried this helper but sadly it gives off the exact same result. although now i got some precise numbers.
what I find is that I have an offset in : x (horizontal) : -16,… , y (vertical) : -5,… in local coordinates from the parent container (not the canva, the canva is one step above).

7900813--1006570--SationBased - SampleScene - Windows, Mac, Linux - Unity 2021.2.9f1 Personal DX11_6.jpg
When I put my cursor exactly on the start of the line renderer, it gives me (-16,-5) instead of 0,0 (local coordinates returned by the RectTransformHelper).
And when I add 2+2, it’s the same offset I have from the top end of my graph.

I’m not sure how I’m messing this up really.

Actually, it works just fine.
Both my initial script with the clucky axis manipulation, and your solution, which is way more elegant.
I zctually needed to cleanup the scales and positions. I didn’t see that my linerenderer object had weird scales (903.xx across all axis) and I was using world coordinates for the lineRenderer points.
I set all scales to 1, use proper local coordianates that makes more sense given that the graph is drawn between 3 local empty gameobject (graph limits) all in the same parent.

I ended up with just this code.

var pointLocal = graphLine.Line.GetPosition(i);
RectTransformUtility.ScreenPointToLocalPointInRectangle(anchorRect, pointerOnScreen, camera, out var projectedFromRectPlane);
var pointDistance = Vector2.Distance(projectedFromRectPlane, pointLocal);

and it works like a charm.
Thanks a lot.

1 Like