- Home /
Question by
snowwolf866 · Aug 23, 2020 at 11:13 AM ·
linerendererrecttransform
Function ScreenPointToLocalPointInRectangle returns different position values.
Hi guys! Totally don't know why I'm getting two different values using RectTransformUtility.ScreenPointToLocalPointInRectangle()
. I want to draw a line in UI using UILineRenderer and everything works fine except first click of mouse after start. For example: (press start game) -> mouse cursor stays in the exact same place -> click -> returns (-1000,-800) -> second click -> returns (200, 100) - which is the correct position and after this first use everything works fine. Here is my script code:
void Start()
{
var pointList = new List<Vector2>(lineRenderer.Points);
pointList.Add(startPoint);
pointList.Add(dragPoint);
pointList.Add(finalPoint);
lineRenderer.Points = pointList.ToArray();
rt = canvas.GetComponent<RectTransform>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
lineRenderer.enabled = true;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rt, Input.mousePosition, cam, out startPoint);
lineRenderer.Points[0] = startPoint;
}
if(Input.GetMouseButton(0))
{
if (finalPoint == Vector2.zero)
{
RectTransformUtility.ScreenPointToLocalPointInRectangle(rt, Input.mousePosition, cam, out dragPoint);
lineRenderer.Points[1] = dragPoint;
lineRenderer.Points[2] = dragPoint;
}
else
{
RectTransformUtility.ScreenPointToLocalPointInRectangle(rt, Input.mousePosition, cam, out finalPoint);
lineRenderer.Points[2] = finalPoint;
}
}
}
Comment
Answer by snowwolf866 · Aug 23, 2020 at 11:40 AM
Topic to close. Problem solved. I just set the Canvas Scaler.