Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UI LineRenderer drawing line not at mouse position

Discussion in 'UGUI & TextMesh Pro' started by Ramcat, May 29, 2017.

  1. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    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:
    Code (CSharp):
    1.  
    2.    [SerializeField] UILineRenderer _FuelUse;
    3.  
    4.    public void HandleMouseDown()
    5.    {
    6.        _Down = Input.mousePosition;
    7.        _FuelUse.transform.position = new Vector3(_Down.x, _Down.y);
    8.        var pointList = new List<Vector2>
    9.        {
    10.            new Vector2(_Down.x, _Down.y)
    11.        };
    12.        _FuelUse.Points = pointList.ToArray();
    13.    }
    14.  
    15.    private void Update()
    16.    {
    17.        if (Input.GetMouseButton(0))
    18.        {
    19.            var pointList = new List<Vector2>
    20.            {
    21.                new Vector2(_Down.x, _Down.y),
    22.                new Vector2(Input.mousePosition.x, Input.mousePosition.y)
    23.            };
    24.            _FuelUse.Points = pointList.ToArray();
    25.        }
    26.    }
    27.  
    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?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.
     
    Ramcat likes this.