Saturday, 27 March 2010

Last bit of the HUD

Now that I have all my variables up and running in the Game screen, I need to make it look a bit nicer. Earlier on, I had made a 2D image to be applied behind the variables in the HUD to make it stand out a bit more.

Something that should also be noted is that I am working for the resolution 1280 x 720 for my game, since this is a supported HDTV resolution (which would make it compatible with most widescreen televisions). Again, if there is time at the end of the project, I would like to go back and set up scaling for all of the images I am using so that the resolution of the game would not affect its appearance.

To add an image to the gamescreen, a GUITexture gameobject must be added. The asset of the image to be used is assigned as the texture, which can then be scaled and positions using hte Pixel Inset functions. The final HUD can be seen below.


Friday, 26 March 2010

Health Bar

Quickly implemented a healthbar in the top corner. It works by creating a rectangle, with a constant height and the width set to the current health of the car. When the health decreases, so does the reactangle width. The current code for the healthbar is:

healthbarRect = Rect(1032, 42, (health / totalHealth) * 225, 20);

Wednesday, 24 March 2010

Timer

Next stage of the HUD to do... is the timer. Basically, I am implementing a timer in the format minutes:seconds:milliseconds which tells the user how long they have been racing (and also to work out methods such as lap time).

I found another nice tutorial at:

http://answers.unity3d.com/questions/4009/how-to-make-a-timer-in-the-game

It basically uses the Time function built into unity to get a reading in minutes, seconds etc. The code is written for the OnGUI() method so I needed to convert in so that it runs in the Update method (so that I can pass the timer readings to a GUIText).

The final outcome is below, but there are some problems with the timer at the moment. For instance, the minutes increase when seconds = 30 and not 60. I triend to fix this by doubling the reading of the minutes, but this results in the minutes increasing by 1 after the first 60 seconds, and then again every 120 seconds after.

private var startTime;
var textTime : GUIText;

function Awake() {

startTime = Time.time;

}

function Update() {
var guiTime = Time.time - startTime;

var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;

textTime.text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
If I find a fix for this then expect an update.

Saturday, 20 March 2010

Speed counter

Now that the lap counter was done, I moved next onto the speed counter. My first thoughts were that this would be quite tricky, but then I discovered that the rigidbody attachment for the car comes with a built in velocity component (thank god).

I found a nice tutorial for making a speedometer at:

http://answers.unity3d.com/questions/8120/how-can-i-make-an-on-screen-speedometer

It basically converts the velocity of the rigidbody into a MPH reading. Had to make a few changes though, since the MPH were being printed out as a float and not an integer. To solve this, i added the parseInt() method to the MPH reading to convert it into an integer value. The code is below.

var mph = parseInt(rigidbody.velocity.magnitude * 2.237);
mphDisplay.text = mph + " MPH";

If I have time an the end, I would like to go make and make a needle based speedometer, but is it not the most important thing to impliment at the moment.

Monday, 15 March 2010

Starting the HUD

Now that the driving mechanics (minus the small bugs) and the waypoint system are down, I can now begin to make a start on the HUD. With the waypoint implementation still fresh in my head, I decided to begin with showing the number of laps the user has completed on the screen.

In the waypoints script, I have a static variable names NoLaps. This is the variable I am using to count the number of laps the players has driven round the course. In order to print this onto the screen, I need to introduce a GUIText into the scene.

After adding the GUIText, I needed to edit a few parts of the Waypoints script to print the variable 'NoLaps' to the GUIText. The first thing to do was to add a new variable.

var lapDisplay : GUIText;
This sets up a variable of type GUIText. Then I needed to create an update method and add a line of code to pass this new GUIText variable the interger value of the 'NoLaps' variable.

function Update () {
lapDisplay.text ="Lap " + NoLaps + "/5";
}

This line is pretty much self explanatory. It passes the NoLaps variable as well as some useful HUD text to the text component of the GUIText variable. All that is left is to Assign the GUIText game object to the lapDisplay variable in the script.

Friday, 12 March 2010

Lap System

After much tinkering and a bit of frustration, I have managed to implement a lap system along with waypoints (to make sure the player goes round the track instead of reversing back and forth over the line.

The lap system works by 3 waypoints set along the track (a waypoint being a cube with an attached box collider as a trigger). When the car collides with a waypoint, a boolean value connected that waypoint is set to true. Once all waypoints have been activated, the number of laps is increased by 1 and all booleans are set back to false. I've added the code below:

var Waypoint0Col = false;
var Waypoint1Col = false;
var Waypoint2Col = false;

static var NoLaps = 0;

function OnTriggerEnter(other : Collider)
{
if(other.gameObject.name == "Waypoint_0"){
Waypoint0Col = true;}
if(other.gameObject.name == "Waypoint_1"){
Waypoint1Col = true;}
if(other.gameObject.name == "Waypoint_1"){
Waypoint2Col = true;}
if(Waypoint0Col == true && Waypoint1Col == true && Waypoint2Col == true){
NoLaps += 1;
Waypoint0Col = false;
Waypoint1Col = false;
Waypoint2Col = false;
}
}
The main diffulty I noticed is that most of the code for Unity is written in Javascipt. While I know C# (which is supported in unity), C++ and Java, there are quite a few methods in Java which I need to keep looking up to find out how to run that particular method (in this case, the OnTriggerEnter method). Converting this code into C# would take too much time so its slightly difficult writing new bits of code in a language I am not perficient in. Hopefully, this won't delay my scedule too much, but it is yet another thing I did not account for in my planning.

Thursday, 4 March 2010

Car Mechanics Update

Spent a bit more time in tweeking the car mechanics to get it closer to what I want it to handle like. Despite being happy with the handling of the dune buddy, there are still several problems with it. The major problem that is with the dune buggy is that it does not drive in a straight line, and instead veers of slightly to the right. I havev spent a lot of time looking into this problem, such as adjusting the wheel colliders, looking at the script, re-importing the models and also posting on the unity forums for assistance, but still can't seem to get it working correctly.

Another problem that is evident in the dune buggy is the camera. Despite the camera following the car, it has moments when the vue flickers from the back view to the side view of the car. I believe the problem may lie with the camera trouble I encountered earlier, with the camera facing the side of the car and not the back. Again, I have looked at the script and cannot find where the problem lies. If perhaps may be easier to fix if it didn't occur at random moments in the game, leaving me unable to find the source that triggers the problem.

However, I cannot spend much more time on the driving mechanics as I need to make a start on other parts of the game in order to keep a scedule. If there is any time at the end of the debugging then I would like to go back and address this problem.

Monday, 1 March 2010

Track finished (again)

Now that I am using the Unity engine to make the track instead of blender, I have changed the design of the track from the original. The reason being so if that (as far as I know), it is not possible to make a cave in the terrain tool, leading to the removal of this feature. Also, I have changed the layout and appearance of the track to a forest, complete with a waterfall.

The track was made using the terrain tool, and the heights were adjusted using the various height brushes. Foliage such as trees were placed by importing the assets which were included in the 'standad assets folder, and textures were placed using the texture brush. To make the river, a shallow ditch was made using the height mapper, and a water mesh was created using the 'simple water' asset included. Particles were used to create the waterfall, as well as the spray created at the bottom. To help make the track, the following tutorial was referred to:

http://www.unifycommunity.com/wiki/index.php?title=Terrain_tutorial.

Images of the track can be seen below.