UI LineRenderer drawing line not at mouse position

I’m using the UI LineRenderer and can’t figure out why it seems to be drawing the line at some scaled position.

My code:

   [SerializeField] UILineRenderer _FuelUse;

   public void HandleMouseDown()
   {
       _Down = Input.mousePosition;
       _FuelUse.transform.position = new Vector3(_Down.x, _Down.y);
       var pointList = new List<Vector2>
       {
           new Vector2(_Down.x, _Down.y)
       };
       _FuelUse.Points = pointList.ToArray();
   }

   private void Update()
   {
       if (Input.GetMouseButton(0))
       {
           var pointList = new List<Vector2>
           {
               new Vector2(_Down.x, _Down.y),
               new Vector2(Input.mousePosition.x, Input.mousePosition.y)
           };
           _FuelUse.Points = pointList.ToArray();
       }
   }

You can see I’m moving the LineRenderer game object around on the canvas - it shows up exactly where I click but the line it draws renders up and to the right of where I click. You can see in the image below, the “dot” and circle (on the scene tab). That is where I clicked but the line is above and right of the click point.

Any thoughts?

I think that may be because you’re using the mouse position for world coordinates. If you use Camera.main.ScreenToWorldPoint() on the mouse position, that may fix it for you.

1 Like