Did you know I open-sourced Energy Hook? As you'd expect, I'm proud of parts of the codebase and mortally embarrassed by other parts, but I'm getting over my embarrassment for anyone who wants to check it out. You can download the project here, the link is at the bottom of the page: http://www.happionlabs.com/energy-hook/ So if you want to dig through for the code that goes with this little article, you can.
Anyways. Not too long ago I did a job interview where they asked me, "Can you talk about a time when you simplified a system?"
On the spot, I couldn't come up with anything good--my best answer was about how the monster leveling in Dungeon Life went from a complex and enigmatic algorithm that tried to gauge the strength of the heroes and multiplied it by how long the current heroes had been playing to a simpler system that just gradually increased the power of the monsters as the game went on--but in hindsight I could have mentioned Energy Hook's wall-running.
When I first implemented the wall-running, I did something rather complex - once I detected that a player wanted to wall-run (are they colliding with a wall and holding down the wall-run button?) I would constrain the avatar to that wall. Much like when swinging I would update the player position and then correct (as described here: https://gamedevelopment.tutsplus.com/tutorials/swinging-physics-for-player-movement-as-seen-in-spider-man-2-and-energy-hook--gamedev-8782) but instead of correcting by tethering to a sphere I corrected by constraining to the wall plane. Once the player collided with another wall or wasn't colliding with anything (they'd run past the edge) I'd turn the constraint off. The code was not pretty.
And then it occurred to me: I only have one entity in my game and Unity has this global gravity vector that we can change. So I stripped out all that code and simply changed gravity when the player is wall-running; the wall becomes the floor (or a sloping floor, really, because I didn't want the avatar to be perfectly vertical) and all the code that works for running on slopes and surfaces just worked and had fewer artifacts. I still had to be able to tell when the wall run was over - if you ended up catching air, or if your velocity suddenly changed (from bonking into something) I'd return to gravity to normal.
How would I do it with Roblox, you might be wondering. Roblox's gravity constant always points straight down, for one thing, and for another Roblox is multiplayer by default. How would I give each character their own gravity? So it wouldn't be as simple but I would probably do it by setting the global gravity to nothing and give each character or entity I wanted to process physics on a gravity BodyForce, so when a given entity needed to wallrun I could adjust its own personal gravity.
I'm sure other devs have independently done the same thing, but that didn't make the insight any less pleasing.
Recent Comments