This simple rope was created for my game, but it is quite generic, so I decided to opensource it. You can use it however you like.
It is made on Unity 2021.3.12, but should also work well on lower or Unity higher versions.
The rope visual is made with a simple Line Renderer.
To create a curve, I use a Quadratic Bézier, drawing an arc between the two rope attachment points and the Middle Point
. The Middle Point
will also be shifted down, depending on the length of the rope and the distance between the rope attachment points. You can freely set the Length
and the number of Segments
for the Line Renderer in the Inspector.
Time to reveal the secret - the Middle Point
is not really used to create the curve. Instead, another point is taken, which tends to take the position of the Middle one.
For this interpolation to be plausible, it must take into account not only the distance to the target, but also the speed (a derivative of distance) and acceleration (a derivative of speed). In general, we need to solve the next second-order equation:
For this, the Euler Method was used, namely its version - the Semi-implicit Euler Method. The result, though physically not entirely correct, looks quite plausible.
You can control three parameters:
Frequency
- oscillation frequency (higher value - stronger oscillation and faster reaction)Damping
- attenuation of the system (less damping - longer attenuation)Response
- system reaction speed (0 - smooth start, 1 - abrupt start, -1 - also an abrupt start, but in the opposite direction)
Here is how changing these parameters affects the behavior of the rope:
You can make smooth following in the same way. An example of it is also included in the repository.
If you like this Rope, maybe you will like my game as well 😉