Eight days. It’s been eight days since I last updated this blog, and incidentally that is exactly how long it took me to fix a goofy issue with ledges in Unreal Engine 4 (UE4).
So UE4 has this really nice pre-made physics engine and character movement engine, and also a bunch of ways to tweak them. A lot of the really basic movement like walking and jumping come prepackaged in UE4, and all I had to do was make sure the values for speed, jump height, etc. felt how I wanted them. Unfortunately, one of the things UE4’s pre-built system is not very good about handling is ledges.
Before talking about this ledge problem, let’s talk about collision detection in UE4. Collision detection is (generally) what keeps you from falling through the floor, walking through walls, or jumping through ceilings. There is also collision detection involved in hitboxes, but for now I’m only talking about the stuff I said before. In UE4, whenever you make a character, it defaults to using a capsule.
You can see what I mean here, it’s the yellow pill shape above. Using a capsule instead of a box is a pretty common practice, as far as I can tell. It helps the engine deal with stuff like walking up slopes or walking off ledges in a smooth way. The problem I ran into, however, is when you’re jumping and don’t quite land on a ledge, you bounce off. Here’s an example of what I mean.
What’s happening here is that the capsule’s rounded edge hits the ledge (pictured below), and bounces off. It transfers a lot of the downward (or upward) momentum outwards, which is a pretty reasonable thing for a physics engine to do. In-game, however, it feels pretty awful whenever this happens, and it happens pretty often. Just imagine in Smash Bros. trying to recover back onto the stage, barely miss landing on the stage, and instead you get shot way off to your death. Not good!
There’s not any great way to stop this from happening, since it’s tied pretty intimately to how the character’s physics behave. After trying several ideas, I ended up finding a solution that kind of worked. It wasn’t ideal, but it was a start.
Online I read about some people who simply set the character’s x velocity to zero (the left-right axis) whenever they hit the bottom or top of the capsule. I tried it, and it…somewhat worked? The problem was that more often than not it made the character get stuck on the ledge, unable to quite make it over the lip so that it could resume falling properly.
After trying out many, many ideas (some good, some stupid, some crazy) the one that finally worked pretty well was making it so whenever the bottom or top of the capsule got hit, the character would overwrite his new velocity (the velocity from bumping off the ledge) with the old velocity (the velocity from right before bumping into the ledge). You can see what it looks like here. Pretty good!
Except…this created some new, weird problems. One of the stranger things was that bumping into a sloped ceiling now made you get attached to the ceiling, like so.
After trying another slew of complicated ideas, what ended up working was a pretty simple fix (sigh). All I had to do was manually update the old velocity’s z value to account for gravity (basically just reducing the z value slightly), plus bump the character very slightly away from the edge. The combination of these two things turned this into this. Whoo!
Back to animating.