Introduction
In this article I present a solution to automatically create cables that hang from two or more points. The system allows us to modify the thickness of the cable, the curvature and more parameters to achieve the result we want.
The wire is drawn using Unity’s Line Renderer component so it is generated procedurally using mathematical expressions.
Unity Package
The solution consists of a Unity package that you can import into your project, this will create a new folder in your project with the Assets that come in the package.
How the Wiring System Works
Figure 1 shows the Assets that come in the package. In the scene you can see a power line that I generated using the wiring system.
To start using the system we can drag the prefabricated “GDT Wire Container” into the hierarchy (blue cube in figure 1). If you can’t create a new Empty GameObject and assign it the “GDTWiringSystem” script this will automatically generate everything needed for the system to work.
The object assigned to the “GDTWiringSystem” script will be the one to draw the cable and its children will be the points from which the cable will be hung.
In figure 2 we see the “GDTWiringSystem” component, here is all that is needed to shape the wiring.
Adding new points for the cable
To create a new holding point for the cable, simply right click on the container object and then click on Create Empty, as shown in Figure 3. This will create a new child and the script will automatically name it.
I do not recommend creating new children by duplicating existing ones, as this produces internal errors.
In figure 4 we see how when creating a new point, a new section of cable appears.
Wire loops
If we want to generate a cable with its ends connected we can activate the option “loopWire”, this will generate a new stretch of cable that joins the last point with the first one.
In figure 6 we see that the cable has been closed.
Wire Smoothness
The subdivision parameter allows us to choose the amount of segments that will be between two points of the cable, initially it is 1, which implies that the points of the cable are joined with straight lines, for the cable to have curvature we must choose a subdivision parameter greater than 1.
The larger the subdivisions, the smoother the cable curvature. The maximum range is set to 15 but it can be increased by opening the script and modifying the “[Range(1,15)]” instruction with the required value.
Cable Curvature
To modify the concavity of the cable, that is to say how intense its curvature is, we have the Sliders inside the “Curvature Parameters” vector, as we see in figure 8.
The default range of these sliders is -3 to 3, but if necessary you can modify it from the script in the “[Range(-3f,3f)]” instruction by typing the required value.
Animation
In case you need to animate the cable, I added a bool called “isAnimatedWire”, when you activate it the cable will be drawn in each FixedUpdate execution that by default is executed every 20 mili seconds.
Result
Using this solution we can automatically draw cables suspended from two or more points. We can add as many points as we want and modify the curvature of each section individually.
The cable is drawn with the LineRenderer component and using mathematical expressions.