Welcome, gather round and take a seat, as I fill this dark chrome window with half-intelligible war-stories of trying to make a rope, a simple rope, that hangs beneath a helicopter… It’s been a long summer of solo-dev, and it’s time to open the kimono.
Why A Rope?
I think the coolest part of having a helicopter in a game is that it’s still able to interact with things on the ground. And the rope just expands those possibilities.
There’s a lot of interesting gameplay scenarios that involve moving things around: people, vehicles, supplies, etc. And I want the game to be very open ended as far as what you can pick up and how you use it. So we need a rope, and it needs to be physics based.
Enter Particle Physics
My first attempt used a rope that was simulated with particle physics. It also had a magnet that could shoot out towards your crosshair. But the rope was limited in length, and often times you wouldn’t hit what you wanted to.
The problems with this approach were:
- It was very performance intensive
- Objects would swing way too much, and placing things precisely was impossible
- Objects would swing around and on top of the helicopter and land on the rotor blades
- Connecting to objects was frustrating and time consuming
No Rope, Best Rope?
After a trip home where I got sick and took a full week off, I returned with fresh eyes and had an idea: what if I separated the rope from the actual physics of carrying things? Then I could code the behavior directly, and just add a purely cosmetic rope to make it look right.
Had I been searching for the best rope, but really what I needed wasn’t a rope at all? I took a deep breath, deleted the rope from the helicopter object and stepped into the void.
I thought, if I’m not just using a rope, and designing the behavior from scratch, what actually is the desired behavior?
- Objects under the helicopter are highlighted so you know which one you’ll pick up
- Once you select an object, you know it’s the one that will be picked up
- Objects are pulled in and hang mostly underneath the helicopter
- Objects don’t bounce or swing too much, so they can moved and placed effectively
- Objects under a certain mass should move similarly, but really heavy objects should move more slowly, or should slow down the helicopter.
On a whim, I decided to try a PID algorithm to move the objects directly.
WTF is a PID?
The PID algorithm is one of those things that sounds complicated because it’s an acronym (also when you call it an algorithm), but it’s actually fairly simple. It’s just a way to move something to a target, and being able to control how fast it gets there and how much it slows down as it gets closer. PID stands for the 3 parameters you can tweak: proportional, integral, differential.
You can use PID control for many different things; it’s used by thermostats to get the temperature right, used by robots to move objects, etc. I was an electrical engineer in a past life, so that’s where I heard about it.
So how do we use it here? We have a PID controller that tries to move an object to a place under the helicopter. We have another one to add torque (change the angle) of the object, so it sways with movement. In the Unity Debug view it looks like this:
The green circle shows the range of objects that can be picked up, and the magenta line shows the force being added to the cube once it’s picked up.
Then we connect the helicopter and magnet with a “cosmetic” rope, and it looks pretty good. This rope still uses particle physics but is much more performant.
What Can you Pick Up?
- Vehicles (even when being driven by AI)
- NPCs, both friendly, enemies, and civilians/VIPs.
- Inventory Supply crates, which give you additional ammo or other items once reeled it.
- Any kind of movable objects (burned out cars, crates, etc).
To make all of these possible, there were some edge cases to cover.
- NPCs have to be “unpinned” so they behave as a ragdoll
- NPCs are carried by their hips, but if you calculate the force by using their hip mass, it’s not enough. So we need a way to override the mass used by the PID in certain cases.
- Vehicles should generally work but we don’t want to flip them over
- Inventory Supply crates should give us ammo and then get destroyed cleanly
To solve this, each object that can be picked up has a PickupRoot component on it, that can declare it’s type, mass, and can be inherited from for the things that need special care. For example, there is a PickupRootNPC that can unpin the NPC when picked up.
NPCs can be picked up, and actually still animate while being carried around. Should they still shoot at enemies while hanging?
What Do You Want To Know?
Ok, you read this far. You’re a pro. What do you want to hear more about? What should I cover next time? Hearing from you is motivating, so let me know!
If you’re interested in eventually getting a steam key to play a closed beta, you can sign up for my email list: https://clearedhotgame.com/email-list/ I don’t have a date set for this yet, but you will be among the first to get access.