Sunday, September 1, 2013

Out With The Old (Character Controller)...

It's about character...

I wasn't entirely happy with the FPS character controller that comes stock with Unity.  While it does a good job of giving you an FPS-style feel (near-instant acceleration, turning on a dime, etc), it removes the opportunity for a bunch of physics-style gameplay as a result.  It was also total crap for smooth climbing in a cube-based world, and for some reason was often not detected properly by the block colliders.  For ZOMG Rockets!, I wanted all the physics-y fun of pure physics objects, FPS-style control, and smooth auto-climbing of one-block incremental height terrain.  So obviously something had to be done.
So I built a new character motor and input controller.  I tried to interfere with the built-in physics of the object as little as possible.  This was a bit tricky, since some of the characteristics of your standard FPS controller are decidedly against the laws of physics.  Here's how I put it together:

  • The character's attempted movement direction is entered as an impulse vector.
  • Force is applied every frame based on the current velocity compared to the maximum defined velocity and the impulse vector.  More force is applied when you're moving slower, or moving in a different direction (>90 degrees difference) than the direction you're trying to move in.
  • To speed directional changes, if grounded (standing on a surface), velocity is reduced by 20% each frame.  To allow ragdolling and being knocked around by explosions, this is suppressed temporarily when hit by an explosive blast.  It's a bit of a hack, but it works pretty well.
  • During each frame, rays are cast in the intended move direction.  If there is ground ahead higher than the player's feet, but below the maximum step height, the player object is moved up part of the way toward the new height each frame.  This is the only direct manipulation of position that is done by the controller, and it is only applied if the character is near stationary, or attempting to move in the direction it's already going.
The overall result feels pretty natural, and should serve pretty well.  And since it's using a standard capsule collider and rigidbody, it interacts beautifully with other objects for block collisions and explosion ragdolling.  I also managed to resolve the "falling through the world while building under your feet" bug (I hope - let me know if you still see it).

...and explosions

I also made a couple of adjustments to explosions.  The explosive force has been expanded and tapered somewhat - explosions now throw things around slightly outside their damage zone, and apply that force in a slightly diminishing fashion (full force at center, half force at the outside edge).  This results in more ragdolling, without being ridiculous (during testing, I accidentally set the explosion force so high I lost sight of the ground when I got launched).

Cave-ins are looking better than ever:

As always, you can play the latest build here.