Monday, June 6, 2011

Unity: Player Movement Physics

I took a break from fixing the inter-level portals (and holidaying in happyrainbowland), and decided to focus a bit on cleaning up player movement. There's been a few small things really bugging me about the physical properties of the player:
  • Hovering at the jump apex when hitting a ceiling.
  • Sticking to the under-side of moving platforms.
Both problems are kind of related, or at least have similar visual symptoms. However, they require entirely different solutions.

The case of hovering was easy to fix: remove any continuous upwards force when the player hits a ceiling. This involved tracking down a couple of lines of code and nuking them. And the result was perfect - no longer can the player hold jump to grapple on low colliders.

The case of sticking to platforms however, is much harder to fix. At least, as far as my understanding goes. It's a combination of the CharacterController component settings and the physics engine. Neither of which I have a great deal of control over.

I tried tweaking the skinWidth variable for the CharacterController, with varying degrees of success. Decreasing the width (which decreases the collision tolerance) resulted in many more visible errors. However increasing the width didn't particularly improve the situation at all. I gave up when the player was hovering two feet away from every surface.

The second attempt involved tweaking collision parameters for the platforms themselves: things like changing the collision type from discrete to continuous, and fooling around with other settings. But this didn't have any noticeable impact either.

I'm trying to solve the issue in code at the moment, by detecting a headbutt and correcting it. Detecting is easy: Unity has a bunch of built-in methods and properties to help you do that. I can reliably detect a headbutt every single time. But correcting it? Not so easy.

I've tried resetting all of the velocities of the player object on a headbutt, which gave a very slight improvement, but nothing to write home about. Then I tried reflecting the vertical velocity to actually rebound the player away from the surface, but that just seemed to make it worse (and I really have trouble comprehending why). But other than these things, I'm really not sure what to do.

I have access to the platform that the player has collided with, so perhaps I can simply temporarily disable collisions between the two? I have to say, physics is not my best asset. That is, the understanding of physics. Actual physics I'm perfectly good at. At least, I can't recall the last time I didn't sink in the bath.

No comments:

Post a Comment