Sunday, February 22, 2015

Game progress: Defender Blender

This was a really busy week, however I managed to have a little progress with Defender Blender.
Defender Blender is a game I made for learning Unity, I'm still learning from it and experimenting playability, difficulty, and a some game design. I will talk more about it once I upload it here, for now I'm sharing the progress I had this week.

First lesson learned, after update Unity 3.2.something to 3.6.2 I found that my game was not working anymore, most of the collisions where working just sometimes so I was not able to hit the enemies, the player was not pushed to stay in the screen (it is pushed by an object at the left of the screen), and things like that, so I started to investigate and found that there where two reasons for these problems:

    1. Rigidbodies sleep. Seems like this is an old Unity feature (before 3.2 version), so I'm not sure how my game was working before :). In order to optimize the physics in Unity, the engine does not check for collisions unless they are required, i.e. only if any object's property that could affect the physics changes. Why my objects were not detecting some collisions? Let me explain.
    • You can have a Collider with Rigidbody, or without Rigidbody. A Collider without Rigidbody can actually collide with other Rigidbodies but only if they are awaken. A Rigidbody won't wake up when touched by a Collider. Some people use a kind of workaround for this (however, note that this is not a bug, it's a feature), they change the Rigidbody's position in the Update method in order to wake it up, something like: "rigidbody.transform = rigidbody.transform". The problem with this is that it is a kind of dirty trick that won't help your game's performance. So what is the correct solution?
    • When a Rigidbody touch another Rigidbody, it wakes up. So put a Rigidbody component in whatever objects you're going to move constantly (like the player, the enemies, the projectiles). Adding a Rigidbody to some objects fixed my issues with some collisions.

    2. Methods in animations. After adding the Rigidbodies to some of my components I was still having problems to attack my enemies, it was like there was a kind of random factor that could make it work, or sometimes delay the collision. Actually I was able to see the collision happening in OnTriggerEnter method, however it was still not working. Then I found out that the issue was because the logic I used to activate the collision of the player's weapon. I used a method call from an Animation as you can see in the screenshot:
StartAttack() method activates a boolean variable that decides whether the sword collision is going to happen or not, this was needed because even when the Collider was really small when not attacking, sometimes the enemies just died because they touched the Collider inside the character.
So the plan was: when the animation starts, call StartAttack(), so the collisions are processed...
Well that is how it worked before, however animations seem to not have a lot of priority for Unity in processing, they are visual components, it's not a really good plan to depend on their accuracy to fire events in the right moment. Instead I called StartAttack() from the method which starts the animation. That fixed my problem.

What is I was really planning to do
After solving these bugs I started what I actually was trying to do, I added a new component to my game: Walls. I'm planning to use them mainly to teach the player how to jump because I noticed some people struggled to notice that keep the jump-key pressed make the character to jump higher. Also I want to use the walls to teach some other movements in the game, I will use them in a new How-to-play level.
Since my game uses physic objects as Kinematic objects, it does not use the physics engine to affect the objects movement, it only detects the collisions, so add a wall is not as easy as drop a rectangular Collider in there.

The Kinematic Rigidbodies/Colliders know they are touching each other, however they don't know the direction of the touch, so I can't just program my character to stop moving when touching a wall, because then it will stop as well when walking above the wall (it must be able to work above the walls). I was planning to use a different Collider on the top of wall but it was going to be messy. My plan so far is to do it by comparing the wall's position and the character position. When the player is above the wall, the wall will behave as a platform, and when the player is in any side of the wall, it will act as, well... a wall.

Well this is work in progress, after complete the programming of these walls, I hope to make a post about my how-to-play level design. See you next week!

No comments:

Post a Comment