Wednesday, 5 June 2013

Adding Health to the Character, Respawning and Healthups

Health was added to the Main Character with creating a Health variable that change when it interacted with a script that dealth "damage". Basically, damage is the decreasing of the value assigned to the Health variable, as the health goes lower the GUI image for Health changes. Overall I have 11 images for health, when the Health variable is at 100, the healthbar looks full, when it is at 0 it is empty.
Additionally I managed to set the script that when the player ran out of health the script would send a message that would activate a function within my main player controller script that would cause it to respawn at the character respawn point with all it's health. Effectively, if the player were to run out of health or fall out of the game they would have all their health returned to them and be brought back to the spawn point. To the right is an image of the Health at 50% when the character has taken some damage from an object with the damage script. (Bare in mind this is not the final resolution for the game).

Furthermore I've managed to create a script that would add a value to the health when the character would interact with it. The object itself acts as a type of Health pickup and is destroyed when the main player touches it. Similarly the same method is used with the checkpoint as I have both object rotating in mid-air, they both get destroyed on contact but do different functions.
Below is the code for the Health-up script.
function OnTriggerEnter (other : Collider) {
    other.gameObject.SendMessage ("hup30", SendMessageOptions.DontRequireReceiver);
    Destroy (gameObject);
}

function hup30 ()
{
HEALTH = HEALTH + 30;
}

And below this text is the code for the checkpoint script.

var spawnPoint : Transform;

function OnTriggerEnter (other : Collider) {
 if(other.tag == "Player") {
 spawnPoint.position = Vector3(75, 10, 0);
 Destroy(gameObject);
 }

}

Sunday, 2 June 2013

Placing down Platforms, Character Creation and Setting up the Camera

Now, in order for the character to move around the scene we need some physical colliders they can jump to and from, in other words we need some platforms! By placing a few rectangle shape gameobjects into the scene-view I will effectively be creating platforms, what I must make sure though is that they're located along the same Z-axis as one another otherwise they won't be inline. The reasoning to why these default gameobjects will act as platforms is due to their individual box colliders. These box colliders give solid-like properties to these objects when placed within the scene. If these objects didn't have the box colliders then the characters/dynamic shapes would just fall through the platforms and outside the level.
Additionally, the platforms that are touching eachother are put into the same empty parent object which has a "Combine Children" script. This script takes all of the children objects (platforms) and combines all their meshes together. Therefore instead of rendering 6 different objects, only 1 object is rendered saving memory and enhancing game performance.

So far in our game we have the attributes of the level set and a few test-platforms to inhabit it however we're missing our main character! Introducing the prototype character that will be controlled by the user, "Capsule". Capsule isn't like any other default gameobject in Unity, through a little bit of tinkering I've managed to give it the basic movement controls it needs to explore the scene. This is done through the Platform Controller script I attached to it. What this specifically does is that it works alongside with the inbuilt CharacterController component Unity has and defines the physical mechanics within the environment. Additionally, through the Inspector pane on Unity I can optimize the variety of functions the Platform Controller Script inhabits, for example I can change the walk or run speed of the character or jump height, etc.

Even though we now have a simple character moving around an environment we need to introduce the Main Camera otherwise they will just walk offscreen! We need the Main Camera to do the following:
  • We need the camera to follow the character but to also introduce the subtle movement when the character moves further in one direction. The camera in this game is much like the style of games such as Defender, this allows the player to easily view what is ahead of the level before they reach it.
     
  • We also need the camera to track the main character when it jumps by adding a "springiness" property to the camera script. To make it not so jumpy for the player, it is preferred for the camera to not follow the complete motion for when the character jumps. By making the camera lag with the springiness property, we can stop it following the complete jumping motion

In order to achieve these objectives we can add the following scripts to the camera object.
Camera Scrolling: Allows the camera to move how we want with a distance property and a springiness property. The distance property (z-direction), sets the distance away from the target object (our main character).
Springiness Property: Stops the camera from following the complete jumping motion. The value itself defines how responsive the camera is to the target motion.

As well as the basic camera features we also have a few camera features set to our character, this script is called "CameraTargetAttributes".

Height offset: If set to 0, target will be sitting vertically center of the screen. If set to positive number, the camera will shift upwards which would create a vertically off-centre target.
Distance Modifier: We can modify the distance of the camera from the target, this setting adds onto the value that is defined in the camerascrolling script. (This functionality is good for multiple targets).
Vertically look ahead: Defines how quickly the camera shifts to look ahead when the target is moving.
Max look ahead: In order for the character not to leave the screen the value set here is the distance that the camera will stop looking ahead. X is horozontal distance whilst Y is vertical distance.

Setting the Game Attributes

With the creation of my title pages I am now going to start the game development process for my Final Major Project. My first goal with this level is to create an environment that I can walk around and have a few platforms to jump from one to the other.
When creating a game in Unity that is predominantly 2D we need to understand the common principles that will stay consistent in order for the project to function. To start off we need to define our plane of motion for all of the gameobjects. In order to prevent this game from being three dimensional, one of the dimensions must be restricted from motion. For my game, I’m going to choose the X-axis to correspond to horizontal movement and the Y-axis to correspond to vertical movement. The Z-axis will therefore correspond to movement to and from the observer (or camera), this will inhabit no character movement within it. In order to easily memorize the planes of motion, when I click upon a Gameobject within the Scene-view, the red, green and blue arrows represent the directions of X, Y and Z.
The second principle I'm going to be sticking to is the restriction of rotation. The only rotation that will be present within the game will be around the z-axis. This is due to the camera passing through this axis, which results in clockwise and anti-clockwise rotation. The only exception that will be used for this rotation is the Y-axis so he can turn from side to side in mid-air.

In order to help enforce these principles I'm going to use an object called "Level Attributes" that has a script with the same name attached to it. This script does a whole variety of environment setting features, these features do the following:
  • Displays the level's dimensions within the scene-view by drawing a green bordered rectangle.
  • Creates a physical collider that acts as a border to stop objects falling infinitely or users going beyond where they should be going.
  • Allows the user to optimize the position where the physical colliders start and how wide/high they are away from the position.
  • Gives the character room to fall without the camera following it until it hits the bottom collide.
So now we have a script that creates an area for the character to move up and down the X and Y axis, however if the character were to fall to the boundry of this area nothing would happen. This is where we implement the Death Zone object that has the "DeathTrigger" script attached to it. What this script does is that it provides a collider which causes the character to respawn if they fall onto it, this is a great feature for small pits or the whole level itself.