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.

unity assets to generate cables automatically
Fig. 1: The download package contains these assets.

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.

inspector for a unity component to draw cables procedurally
Fig. 2: In the inspector we can modify the wiring parameters.

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.

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

draw cables in unity via script
Fig. 4: A new holding point is created for the cable.

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.

inspector for a unity component to draw cables
Fig. 5: The loopWire option makes the last point of the cable connect to the first point.

In figure 6 we see that the cable has been closed.

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

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.

inspector for a unity component to draw cables
Fig. 7: If we increase the subdivisions we will get smoother curves.

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.

inspector for a unity component to draw cables
Fig. 8: The bending parameters allow us to adjust the concavity of each cable section.

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.

wires in unity with line renderer component
Fig. 9: As the subdivisions are increased, the curvature of the cable can be seen.

Scroll to Top
Secured By miniOrange