Friday, 25 April 2014

Summary

7. Summary and final touches.

To make the game more realistic, player and enemies objects has an attached sound effects triggered in different cases. Player object has attached sounds like: "PistolFire", "ShotgunFire" in case we shoot, "DryClick" in case we want to shoot but we don't have ammunition and "PlayerHit" in case we received damage. On the enemies side we have: "EnemyHit" in case enemy received damage and "EnemyDie" played when enemy dies. Prefabs for ammunition has also attached sounds e.g. "PistolReload" in case when we collect ammunition.

In this blog I tried to describe main functionalities of this project supported with examples. It was really big fun to create something unique, something what actually can give you a lot of satisfaction.

Building my project from scratch helped me to understand how Unity2D works, fundamental Unity interface functions, scripts structure and fundamental C# syntax, relations between game objects and scripts. On graphics side I learned a loot about "Tiled" and "Photoshop".  

In the future, I would like to implement few more functions like: story line, machine gun weapon, health packs, more new enemies with different features and abilities, bosses and more levels.

Complete project available at:
https://github.com/Adammdit/Unity

Screenshot of the game:

  

Thursday, 17 April 2014

User interface, memory leaks

6. User interface objects and memory leaks.

User interface consist of:

- player icon
- health bar
- two icons of weapon
- number of ammunition available
- score

All those elements are represented by objects GUI Texture and GUI Text. Each object has attached script to set optimal position on screen relying on current screen resolution e.g. transform.position = new Vector3(posX, posY, 1); where posX = 0.4f, posY = 0.03f.

Main menu of the game can be recalled by pressing "Esc" key and consists of three options "Resume", "Restart" and "Quit". When "Esc" key is pressed game time is stopped and position of menu is changed to centre of the screen . Menu is drawn on GUI whole time but in different positions. When we want to access the game menu then it position is set to centre of the screen, when we want to close it, its sets it position outside of the screen. If we press "Resume" button, game time is set back to one: Time.timeScale = 1;. For "Restart" option, when the button is pressed, I load scene one and reset global game variables e.g. Application.LoadLevel(1);. For "Quit" option I'm using: Application.Quit(); which will close the game. Last element placed on GUI is screen frame which is a PNG file scaled and position exactly in the centre of screen.
As I progressed with my game I noticed that when the game is running it's constantly consuming computers memory (RAM) until it's full and computer crashed. To fix that I used: Resources.UnloadUnusedAssets(); and I placed it in Update() function.

Game menu:


Screen shoot of my user interface and frame:

Tuesday, 1 April 2014

Enemies

5. Enemies and ammunition as a prefabs.

Ideally in the game I'm going to use three different types of enemies and two different types of ammo as a different prefabs. Prefabs are game objects combining all functions and preferences of particular object e.g. ''Prefab_Enemy1" (prefab of first enemy in the game) prefab will consist of c# script, sounds, animations, translate, rigidbody 2D and all components we attached to particular object. All prefabs are connected to their original object - they are clones. In order to create a prefab simply drag a game object that you created in the scene and drop it in project view. The name of this object will turn blue to show that is a prefab. Now we can rename our prefabs and delete blue objects in hierarchy view (note that if you're going to make any further changes to animations you will have to reverse above order - drag prefab from project view and drop it in hierarchy view apply changes and make the prefab again).  
All enemies are using the same algorithm representing their artificial intelligence. Basically enemy ray-casting against player, if player is in the range then enemy starts walking towards player. Each collision between the player and enemies decreases player's life. To detect collisions between different enemies I used tags, so each enemy has unique name and can deal different damage.

Three types of enemies: