Wednesday, June 8, 2011

Unity: Hazards Working...

...Player response, not so much.

Implementing hazards was super easy.
  1. Add a collider.
  2. Make it a trigger.
  3. OnTriggerStay, call Harm(damage) on the Player.
Right now, the player starts with 5 hitpoints, and falling into the lava on level two takes away a hitpoint every second. Once the player's hitpoints reach zero, they are respawned with 5 hitpoints.

I added some extra control, such as an immunity timer after the player is injurred, giving them a temporary reprieve from more injury. Additionally, I attempted to implement a knockback effect, which throws the player back a little, temporarily disabling controls: A slight penalty for taking damage.

It works, and it doesn't.

The controls accurately disable for a short time. And the knockback kind of works. The problem there is I'm attempting to wrap the knockback effect up in the standard player movement for the sake of tidiness, but it's actually getting quite untidy. So instead, I'm going to implement a simple FSM (finite state machine) that has two states: normal, and harm. In the normal state, we calculate regular movement, but in the harm state we calculate the knockback movement.

But I have a big problem I'm not currently sure how to fix: Respawning the player in the Update() loop doesn't update the player's collision volume, so the lava is triggered one frame after the player respawns, causing the knockback and damage. I am assuming this is just the nature of rigidbodies, and I need to find a way to circumvent the delay to collision-updates.

I was going to post a demo, but it's too buggy. For some reason, the player registers an injury the instant the game starts (even though there's no damagers in the first level at all). I've had a full day, and I have a drive to Sydney tomorrow, so I'm a bit unfocused. I expect part of the problem is a lack of concentration. Coding while distracted is not a good idea.

There's other productive things I can do, anyway.

There's a Minecraft update that needs my attention.

No comments:

Post a Comment