Tuesday, June 7, 2011

Unity: Sticking to Platforms (Fixed)

So, thanks to an awesome guy named James Podesta and the wonders of social networking, the player no longer sticks to the platforms. There are some excellent fringe benefits to befriending genius programmers on Facebook! He played the demo, and immediately identified the problem: When headbutting a platform, I was setting the player's velocity.y to zero. What I should have been doing was setting the player's velocity.y to zero only when the player had been moving into the platform on the contact frame. I was going to use collision normals to fix the problem, but I realised it was easier to just test previous velocity to see if the player had been moving upwards at the time of the headbutt.

So more or less:

void OnHeadbutt()
 if (velocity.Y > 0.0f)
   velocity.Y = 0.0f;

Seriously, as easy as that! I think there is still an occasional sticking problem, when moving at speed horizontally and jumping into the platform. And I think it might be related to the capsule collision object penetrating the platform enough while the platform is also moving downwards that gravity alone is not enough to move the player entirely out of collision. I need to sift through the (now quite messy) code this evening.

As I said in a previous post, a hacky solution might be to disable collisions between the player and the offending platform for a couple of frames. A more complicated solution might be to track the vertical motion of the platform and the player and compare to see if the player needs to be pushed further. It's what I do to keep the player on the platform when they land on it, so I essentially need to reuse that code (with some refactoring of course) to work downwards instead of upwards.

So anyway, a huge thankyou to James for fixing my code on a Facebook wall post! And apologies in advance. I don't think he realises what he's volunteered himself for.

I wonder if he keeps his phone number on his Facebook profile?

Programmers are usually awake at 2am, right?


No comments:

Post a Comment