Search Unity

Easy Curved Line Renderer [Free Utility]

Discussion in 'Works In Progress - Archive' started by Haagndaaz, Mar 12, 2016.

  1. Haagndaaz

    Haagndaaz

    Joined:
    Feb 20, 2013
    Posts:
    232
    Earlier today I discovered AnimationCurves and was experimenting with their usefulness when I discovered how to easily smooth out any set of positions, and so it is also very easy to create some simple Curved Line Renderers!

    Included:

    LineSmoother.cs
    • main utility script for smoothing out positions
    • static function so it can be accessed without being attached to a gameobject
    • feed it an array of Vector3 and a segment size and you will get back a smoothed out array!
    • segment size is world space and so the number of line segments in between points adjusts based on distance between points
    CurvedLineRenderer.cs
    • example use case for smoothed out positions
    • add new points and it will automatically update to include the new position!
    • example scene is provided with a regular curved line and a curved line with physics applied to each point
    CurvedLinePoint.cs
    • draws gizmo and sends back to CurvedLineRenderer to update line when the point is moved
    And because I think this is useful, and because I always enjoy when others post free stuff, I am giving this away for free! :)

    curvedLine001.PNG
     

    Attached Files:

  2. mattmanj17

    mattmanj17

    Joined:
    Jul 22, 2013
    Posts:
    6
    This is a really neat little utility! :)

    I could see people coming back to this post for a while whenever they need something like this.

    It seems one of those things a lot of people have thought "that would be nice to have", but never sat down and wrote.
     
  3. Haagndaaz

    Haagndaaz

    Joined:
    Feb 20, 2013
    Posts:
    232
    Glad it is useful! :) Are there any other tools that you have thought "that would be nice to have"?
     
  4. hanfengsunshine

    hanfengsunshine

    Joined:
    Nov 18, 2016
    Posts:
    1
  5. GustavoZabala

    GustavoZabala

    Joined:
    Aug 10, 2017
    Posts:
    3
    True, more than a year has passed and I already found it useful :)

    If you have a visual editor to handle the points in real time it would be great, it's not that much user friendly when you have to do a precision curve, but it's just a "nice to have" thing really :)
     
  6. R0ma1n

    R0ma1n

    Joined:
    Jul 25, 2017
    Posts:
    1
    Awesome !
     
  7. DTechnology

    DTechnology

    Joined:
    Jan 22, 2016
    Posts:
    1
    Thanks man, this tool is awesome
     
    Haagndaaz likes this.
  8. gkfr

    gkfr

    Joined:
    Nov 28, 2017
    Posts:
    3
    Thanks a lot for this tool :) After struggling for some time to draw curves for objects to follow, you helped the noob i am so much !
    I just changed this to make them grow :

    Code (CSharp):
    1. // 2 public variables instead of 1 for width
    2.     //public float lineWidth = 0.1f;
    3.     public float lineWidthStart = 0.1f;
    4.     public float lineWidthEnd = 0.1f;
    5.  
    6. // then used in SetPointsToLine()
    7.             //line.SetWidth( lineWidth, lineWidth );
    8.             line.startWidth = lineWidthStart;
    9.             line.endWidth = lineWidthEnd;
    Which then allows me to use the Width curve in the editor to finetune the growth
    upload_2017-12-2_11-16-46.png

    Thanks again
     
    koningkees likes this.
  9. Dgreenny

    Dgreenny

    Joined:
    Aug 23, 2013
    Posts:
    2
    this looks great, but what version of Unity is it for? It does not load with 2017.3
     
  10. G_P_V

    G_P_V

    Joined:
    Feb 17, 2017
    Posts:
    10
    I successfully used this very useful package (thanks a lot @Haagndaaz !!!) with Unity 2017.1, 2017.2 and 2017.3.
    I also made some changes to make curved lines look like their were solid (wires or pipes).

    Here you can find a screen capture and a package made with Unity 2017.1.2f1 (also tested in Unity 2017.2 and 2017.3).
     

    Attached Files:

    Last edited: Feb 23, 2018
    bariscigal, jterry, CityGen3D and 2 others like this.
  11. Haagndaaz

    Haagndaaz

    Joined:
    Feb 20, 2013
    Posts:
    232
    Whoa! Glad you liked it and great job in improving it @G_P_V :)
     
    ihgyug likes this.
  12. rightdroid

    rightdroid

    Joined:
    Dec 27, 2017
    Posts:
    2
    Wanted to say thanks too :) Found it very useful in my little Kinect game, generating an elastic line between a character and their thought bubble. Threw a thought bubble material on the line and voila!
     
  13. NaveYllek

    NaveYllek

    Joined:
    Sep 14, 2017
    Posts:
    9
    Thank you for sharing this cool resource ! It's very useful for a project I'm working on. I wanted to know if it's possible to change the Line Width of a specific Line Point instead of the global Curved Line. I basically want it to get thiner at the end of it. Do you know how I could do that ?
     
  14. G_P_V

    G_P_V

    Joined:
    Feb 17, 2017
    Posts:
    10
    Done!
    I added a flag useCustomEndWidth (Use Custom End Width in Editor) to enable a different line ending (set to false by default to not affect the previous version), with an additional endWidth field to be set if the previous flag is set to true (Use Custom End Width checked). I also updated the example scene to demonstrate this feature.
    Here you can find a screenshot of these changes and the updated package:
     

    Attached Files:

    nar3n12, koningkees and Haagndaaz like this.
  15. NaveYllek

    NaveYllek

    Joined:
    Sep 14, 2017
    Posts:
    9
    Perfect ! Thank you very much for this update ! Helps a lot :)
     
  16. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Should put this on the asset store for free, it's a very neat utility
     
    Haagndaaz likes this.
  17. teak123

    teak123

    Joined:
    Jul 27, 2017
    Posts:
    1
    thank you ,it's helpful!!!
     
    Haagndaaz likes this.
  18. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    Please can you provide me 5.4 version of it.
     
  19. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    This is really aweseom and i am looking for. But i imported the package in unity 5.4 it is not working. the error i have remove but there is no object at all.
     
  20. G_P_V

    G_P_V

    Joined:
    Feb 17, 2017
    Posts:
    10
    @MFKJ The version you are trying to use was specifically developed upon Unity 2017.1 and above, it does not work with previous versions because of the lack of line texturing feature. Anyway you can still use the original version by @Haagndaaz.
     
  21. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    But the problem is that your wire is more realistic and i want that feature.
     
  22. G_P_V

    G_P_V

    Joined:
    Feb 17, 2017
    Posts:
    10
  23. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,487
    Hi, sorry if I'm mistaken, can this interesting util be used as a rope that interacts with physics/FPS controller?
     
  24. G_P_V

    G_P_V

    Joined:
    Feb 17, 2017
    Posts:
    10
    In the provided example scene you can find something similar in the "CurvedLine Physics (wire)" game object. It it modeled as a flexible cable, with Spring Joints between each pair of line control points ("LinePoint*" game objects in the example). I think you can use Unity joints to model a behavior of a rope. You should consider that rigid bodies are placed only at the control points, so if you need more accuracy you also need more control points.
     
    Haagndaaz and khos like this.
  25. Romano

    Romano

    Joined:
    Nov 27, 2013
    Posts:
    76
    Anyone know if this is still working in 2018.2.4 and beyond? I've changed the obsolete line:

    //set line settings
    //lineRenderer.SetVertexCount( smoothedPoints.Length );
    lineRenderer.positionCount = smoothedPoints.Length;


    but I can't seem to get the lines curving and the gizmos won't show.
     
  26. G_P_V

    G_P_V

    Joined:
    Feb 17, 2017
    Posts:
    10
    @Romano Which version you downloaded? In the version attached to my previous post I can't find the line you commented.
    I tested it with Unity 2018.2.5 and it works.
     
  27. Romano

    Romano

    Joined:
    Nov 27, 2013
    Posts:
    76
    Ah! I have no idea which version I got it such a long time ago. Is post #14 the most recent?

    Thanks for the reply!
     
  28. G_P_V

    G_P_V

    Joined:
    Feb 17, 2017
    Posts:
    10
    @Romano Yes, as far as I know... I posted my changes only here until now.

    @Haagndaaz Do you host this package somewhere else? If not, do you plan to publish it somehow (e.g. GitHub, Asset Store)?
     
  29. Haagndaaz

    Haagndaaz

    Joined:
    Feb 20, 2013
    Posts:
    232
    Hey @G_P_V, I honestly didn't think about posting it anywhere else, just since I don't have time to maintain it at the moment.
     
  30. G_P_V

    G_P_V

    Joined:
    Feb 17, 2017
    Posts:
    10
    @Haagndaaz I perfectly understand. Would you agree if I publish this package on my GitHub page (https://github.com/gpvigano) under MIT license (https://opensource.org/licenses/MIT), mentioning that you are the original author, with a link to this forum?
    I think this could help to keep track of changes and to collect contributions. Anyway this is your creation, so I'll obey your decision...
     
    Last edited: Oct 4, 2018
  31. Haagndaaz

    Haagndaaz

    Joined:
    Feb 20, 2013
    Posts:
    232
    I wouldn't mind at all! You can also email me at acryliccode@gmail.com if needed :) Have fun with this!
     
  32. G_P_V

    G_P_V

    Joined:
    Feb 17, 2017
    Posts:
    10
    @Haagndaaz: thanks, I created a repository on GitHub for your Easy Curved Line Renderer, mentioning that you are the original author, with a link to this forum: https://github.com/gpvigano/EasyCurvedLine

    Thanks also to every one in this forum who contributed with questions, solutions and suggestions.
     
  33. spoutnix

    spoutnix

    Joined:
    Dec 12, 2015
    Posts:
    8
    Hey guys, thanks a lot for your work ! Really cool features, I used the script to easyly make electric wires, very nice and pretty "cheap" ingame.
     
    G_P_V and Haagndaaz like this.
  34. PJ_Rocks

    PJ_Rocks

    Joined:
    Jul 5, 2016
    Posts:
    1
    Thank you!
     
  35. jd_powell

    jd_powell

    Joined:
    Jul 3, 2015
    Posts:
    5
    Great resource - thank you!
     
  36. whidzee

    whidzee

    Joined:
    Nov 20, 2012
    Posts:
    166
    Thank you @Haagndaaz and @G_P_V very much. This is fantastic

    I love how the curve smoothly enters and exits each point. How would you go about connecting the last node to the first one? to create a loop, so that the spline is smooth the whole way around. I see that i can use the line renderer to create a loop but it adds a straight section between the last and first point.
     
  37. Haagndaaz

    Haagndaaz

    Joined:
    Feb 20, 2013
    Posts:
    232
    That was something I never got around to, making some function to close the loop. If you want, you can go ahead and try to add that in and contribute ;)
     
  38. whidzee

    whidzee

    Joined:
    Nov 20, 2012
    Posts:
    166
    unfortunately my programming skills are still very rudimentary. I'm not liking my chances of solving this

    In the CurvedLineRenderer.cs file I added a public UseLoop bool = false; variable.

    Inside the GetPoints function i added this. It connects the end to complete the loop but i am unable to work out how to use your LineSmoother.cs to smooth out in and out of the start point.

    if (UseLoop == true)
    {
    CurvedLinePoint childPoint = transform.GetChild(0).GetComponent<CurvedLinePoint>();
    curvedLinePoints.Add(childPoint);
    }
     
    Last edited: Feb 26, 2019
  39. cameroncronheimer

    cameroncronheimer

    Joined:
    Apr 10, 2018
    Posts:
    1
    This is godsent
     
  40. Deleted User

    Deleted User

    Guest

    Hi guys. In Unity 2018.3.3f1. I found a lag in LineRenderer in Unity component and animation. While you use animation curve points is late.
    The solution is rewrite script change from Update() to LateUpdate(). After it it is works perfect!
    https://docs.unity3d.com/Manual/ExecutionOrder.html (Animation goes affter Update and becouse this LineRenderer late for a frame)

    P.S. : Thanks Haagndaaz & others for a beautiful plugin!
     

    Attached Files:

    sama-van, Fenikkel, ExtraCat and 3 others like this.
  41. nar3n12

    nar3n12

    Joined:
    Feb 22, 2019
    Posts:
    2
    Thank you for the unitypackage.
     
    Haagndaaz likes this.
  42. Dr-Game

    Dr-Game

    Joined:
    Mar 12, 2015
    Posts:
    161
    How can I draw the line render in UI ,
    the Screen space of canvas: Camera seems work..
    But the Screen space of canvas: Overlay do not work !!
    Can I draw the line render in Screen space of canvas: Overlay???
     
  43. G_P_V

    G_P_V

    Joined:
    Feb 17, 2017
    Posts:
    10
  44. ShoaibAslamTintash

    ShoaibAslamTintash

    Joined:
    Sep 6, 2018
    Posts:
    11
    Thanks for this package.
     
  45. ismail_koroglu

    ismail_koroglu

    Joined:
    May 24, 2013
    Posts:
    6
    Thank you. It's very useful.
     
  46. Venzel

    Venzel

    Joined:
    May 7, 2018
    Posts:
    19
    Thank you very much! Great code!
     
  47. OrinocoE

    OrinocoE

    Joined:
    Jan 4, 2018
    Posts:
    14
    Thank you a lot!
     
  48. shaaafeee

    shaaafeee

    Joined:
    Jun 25, 2016
    Posts:
    9
    Can I achieve SMOOTH line rendering based on the touch input with this?