- Home /
Get Screen Point from local point in rect
I am making a game and the user will have to draw an object like a car, a house, and so on in a specific rectangle that is colored in blue for now. After resizing the rectangle based on the screen dimensions I have to instantiate a line at the position (0, 0) so the player can draw starting from (0, 0). I will store all the positions of the line that the player is going to drag in an array with the length of the width. I want to work only with the positions inside the rectangle that can range only from 0 to width and not to deal with all the screen/world positions. The problem is that when I try to instantiate the line at the position (0, 0) of the rectangle it only works for the displays with the 1080x1920 resolution and not with other types of dimensions for some reason.
Here you can see that the line is instantiated in a random position and the display height is 2244.
But when I change the display resolution to 1080x1920 it works just fine.
I have no idea what is causing this. My Camera is using an Orthographic projection and the Canvas is set to Screen Space - Overlay. This is a simplified version of the code that I'm using:
public Vector3[] corners = new Vector3[4];
void Start()
{
rectangle.GetComponent<RectTransform>().GetWorldCorners(corners);
CreateLine(0, 0, rectangleWidth);
}
public void CreateLine(int x, int y, int width)
{
Vector3 temp = new Vector3(Remap(x, 0, width, corners[0].x, corners[3].x), Remap(y, 0, width, corners[0].y, corners[1].y));
currentLine = Instantiate(linePrefab, Vector3.zero, Quaternion.identity);
lineRenderer = currentLine.GetComponent<LineRenderer>();
lineRenderer.SetPosition(0, RectTransformUtility.WorldToScreenPoint(null, temp));
lineRenderer.SetPosition(1, RectTransformUtility.WorldToScreenPoint(null, temp));
}
private float Remap(float value, float from1, float to1, float from2, float to2)
{
return ((value - from1) / (to1 - from1) * (to2 - from2) + from2);
}
I have been working on this for days now. Any amount of help would be greatly appreciated. Thank you in advance.
Your answer

Follow this Question
Related Questions
Can't get linerenderers to work... 0 Answers
Update LineRenderer every frame (Delete Old Lines) 1 Answer
Line renderer not visible in game 0 Answers