Wiring System for Unity


In this arti­cle I present a solu­tion to auto­mat­i­cal­ly cre­ate cables that hang from two or more points. The sys­tem allows us to mod­i­fy the thick­ness of the cable, the cur­va­ture and more para­me­ters to achieve the result we want.

The wire is drawn using Unity's Line Ren­der­er com­po­nent so it is gen­er­at­ed pro­ce­du­ral­ly using math­e­mat­i­cal expressions.




Unity Package

The solu­tion con­sists of a Uni­ty pack­age that you can import into your project, this will cre­ate a new fold­er in your project with the Assets that come in the package.

GDT Wiring System
GDT Wiring System
Ver­sion: 1
Pub­lished: Jan­u­ary 5, 2020

How the Wiring System Works

Fig­ure 1 shows the Assets that come in the pack­age. In the scene you can see a pow­er line that I gen­er­at­ed using the wiring system.

To start using the sys­tem we can drag the pre­fab­ri­cat­ed "GDT Wire Con­tain­er" into the hier­ar­chy (blue cube in fig­ure 1). If you can't cre­ate a new Emp­ty GameOb­ject and assign it the "GDTWiringSys­tem" script this will auto­mat­i­cal­ly gen­er­ate every­thing need­ed for the sys­tem to work.

unity assets to generate cables automatically
Fig. 1: The down­load pack­age con­tains these assets.

The object assigned to the "GDTWiringSys­tem" script will be the one to draw the cable and its chil­dren will be the points from which the cable will be hung.

In fig­ure 2 we see the "GDTWiringSys­tem" com­po­nent, here is all that is need­ed to shape the wiring.

inspector for a unity component to draw cables procedurally
Fig. 2: In the inspec­tor we can mod­i­fy the wiring parameters.

Adding new points for the cable

To cre­ate a new hold­ing point for the cable, sim­ply right click on the con­tain­er object and then click on Cre­ate Emp­ty, as shown in Fig­ure 3. This will cre­ate a new child and the script will auto­mat­i­cal­ly name it.

I do not rec­om­mend cre­at­ing new chil­dren by dupli­cat­ing exist­ing ones, as this pro­duces inter­nal errors.

hierarchy of a project in unity, create empty gameobject
Fig. 3: To add a new point to the wiring you have to cre­ate a new child in the GameOb­ject container.

draw cables in unity via script
Fig. 4: A new hold­ing point is cre­at­ed for the cable.

In fig­ure 4 we see how when cre­at­ing a new point, a new sec­tion of cable appears.

Wire loops

If we want to gen­er­ate a cable with its ends con­nect­ed we can acti­vate the option "loop­Wire", this will gen­er­ate a new stretch of cable that joins the last point with the first one.

inspector for a unity component to draw cables
Fig. 5: The loop­Wire option makes the last point of the cable con­nect to the first point.

In fig­ure 6 we see that the cable has been closed.

wires in unity with line renderer component
Fig. 6: With the loop­Wire option we see that the cable forms a closed curve.

Wire Smoothness

The sub­di­vi­sion para­me­ter allows us to choose the amount of seg­ments that will be between two points of the cable, ini­tial­ly it is 1, which implies that the points of the cable are joined with straight lines, for the cable to have cur­va­ture we must choose a sub­di­vi­sion para­me­ter greater than 1.

The larg­er the sub­di­vi­sions, the smoother the cable cur­va­ture. The max­i­mum range is set to 15 but it can be increased by open­ing the script and mod­i­fy­ing the "[Range(1,15)]" instruc­tion with the required value.

inspector for a unity component to draw cables
Fig. 7: If we increase the sub­di­vi­sions we will get smoother curves.

Cable Curvature

To mod­i­fy the con­cav­i­ty of the cable, that is to say how intense its cur­va­ture is, we have the Slid­ers inside the "Cur­va­ture Para­me­ters" vec­tor, as we see in fig­ure 8.

The default range of these slid­ers is ‑3 to 3, but if nec­es­sary you can mod­i­fy it from the script in the "[Range(-3f,3f)]" instruc­tion by typ­ing the required value.

inspector for a unity component to draw cables
Fig. 8: The bend­ing para­me­ters allow us to adjust the con­cav­i­ty of each cable section.


In case you need to ani­mate the cable, I added a bool called "isAn­i­mat­ed­Wire", when you acti­vate it the cable will be drawn in each FixedUp­date exe­cu­tion that by default is exe­cut­ed every 20 mili seconds.


Using this solu­tion we can auto­mat­i­cal­ly draw cables sus­pend­ed from two or more points. We can add as many points as we want and mod­i­fy the cur­va­ture of each sec­tion individually.

The cable is drawn with the LineRen­der­er com­po­nent and using math­e­mat­i­cal expressions.

wires in unity with line renderer component
Fig. 9: As the sub­di­vi­sions are increased, the cur­va­ture of the cable can be seen.

Scroll to Top
Secured By miniOrange