I wanted to create dynamic power lines for a game that I’ve been working on in Unity for the last few weeks. I couldn’t find a solution that satisfied my requirements, and so I decided to implement a custom solution, which I wanted to share with you.
My project required me to write a simple script that’s easy to understand, and that doesn’t require a lot of processing power. Furthermore, the power lines should be dynamic, easy to customize, and animated.
For that purpose, I wrote a simple script that generates a certain number of wire segments between a start and an end point. The segments are then linked and connected with joints. Every segment is assigned a variety of properties and elements, one of which is a RigidBody. This allows the script to push a certain number of said segments around as if they were affected by the wind:
The video above shows the script in action. I also utilized the arcball camera from a previous tutorial to make the demo more interactive.
I quickly threw together this relatively short unscripted video that walks you through the code and the general idea behind this project. Underneath the video, you can find the same information in textual form.
Generating wire segments
My script generates a configurable number of individual line segments within the start method. For that purpose, it creates 3D primitives that represent the wire.
By default, it uses cylinders. However, since I created the video, I added an option to generate cubes instead. This helps reduce the overall number of vertices in the scene and should, therefore, help optimize the performance of the game.
Once a segment got generated, it’s scaled to the right length and diameter, moved to the correct position, and then rotated. The first wire segment is generated at the start GameObject’s position, and the wire follows the forward axis of the start GameObject.
Each wire segment has a RigidBody component, and it, therefore, also has a mass. When the number of segments gets too large, the wire might become saggy. To help diminish this effect, every wire segment also has a ConstantForce element attached to it, which, as the name suggests, constantly applies a force to the segment. This effectively reduces the gravity of the entire wire.
Once a wire segment is completely initialized and configured, it’s connected to the previous segment via a hinge joint. The first segment is linked to the start point, and the last wire segment is attached to the endpoint.
Because every line segment has a RigidBody component assigned to it, the entire animation is handled by Unity’s physics engine. I hope that this further minimizes the CPU overhead generated by the wire (Remember: It’s only a backdrop item after all).
The DynamicWire class doesn’t have an Update function. Instead, the entire animation is handled by a co-routine. In this routine, a force is applied to a certain number of wire segments whenever it’s called. This makes the wire swing in one direction, then it remains in that position for a short time, before swinging back the other way. This creates a convincing look as if the wire was swung around by light to modest wind.
The animation can be enabled and disabled at runtime to allow for optimization. For example: When the player moves too far away from the wire, the animation can be turned off to further reduce the overhead created by the wires in the scene.
Download the Code
You can download the code here. The video above explains how you can use it in your Unity Scenes.
Note, that you may only use this code in personal and educational projects. Any commercial use and redistribution is prohibited. If you’d like to use it a commercial product, please contact me first.
Additional things to consider
In theory, these wires are fully dynamic and can be moved around at runtime. However, in practice, that’s not the case, due to the constant force:
You’d need to modify the script if you want to to use it as a dynamic in-game object (e.g. for swinging around).
Note, that the segments will always follow the forward axis of the start point.
With the individual segments, it should be quite easy to customize the code so that other objects can be attached to the wire (e.g. birds sitting on the power lines).
This simple method allows you to generate animated power lines dynamically in Unity. It’s a very simple method, and only really suitable for backdrop items.