Search Unity

Need a linerenderer that works on a canvas

Discussion in 'UI Toolkit' started by Yukken, May 27, 2019.

  1. Yukken

    Yukken

    Joined:
    Jul 12, 2015
    Posts:
    93
    I'm using a linerender to visualize swipes on touch devices. I have a simple script that uses a line renderer that has sorting layer above everything else on the scene. I basically set the starting and end points every frame. The problem is that when the camera moves, the linerenderer lags behind. If there was a way to directly use an overlay canvas to display the linerenderer then I would'n't have any issues.
    The script in question:
    Code (CSharp):
    1. public class ArrowLiner : MonoBehaviour
    2.     {
    3.         public LineRenderer liner;
    4.         public Transform endpoint;
    5.         public SpriteRenderer originSprite;
    6.         public bool startVisible = false;
    7.  
    8.         private void Awake()
    9.         {
    10.             toggle(startVisible);
    11.             liner = GetComponent<LineRenderer>();
    12.         }
    13.  
    14.         public void setDir(Vector2 origin, Vector2 end)
    15.         {
    16.             transform.position = origin;
    17.             endpoint.position =  end;
    18.  
    19.             liner.SetPosition(1, endpoint.localPosition);
    20.             endpoint.allignToDir(end - origin);
    21.         }
    22.  
    23.         public void toggle(bool isOn) {
    24.             liner.enabled = isOn;
    25.             endpoint.gameObject.SetActive(isOn);
    26.             originSprite.enabled = isOn;
    27.             if (!isOn)
    28.                 endpoint.transform.localPosition = Vector3.zero;
    29.         }
    30.     }
    (alignToDir is just a extension method I made to set rotation of a transform along a direction)
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231