Unity: Starting your First Project (Part 4)

Posted: November 22, 2011 in Uncategorized

This is the final article in a 4 part series on starting your first Unity project. This article will provide some basic scripts that are common among different genres. These are meant to be a starting point, and by no means are they complete.

Basic Movement

The following code moves an object by 1 unit in the x or z direction. It is very simple, but can easily be expanded. You can change this to work on a speed variable, and easily add acceleration or other basic physics.


if (Input.GetKeyDown("a") || Input.GetKeyDown("d")){
 deltaX = (Input.GetKeyDown("a")) ? -1 : 1;
}
if (Input.GetKeyDown("s") || Input.GetKeyDown("w")){
 deltaY = (Input.GetKeyDown("s")) ? -1 : 1;
}

transform.position += new Vector3(deltaX,0,deltaY);

This simpy uses the GetKeyDown function of the Input object to determine if the a, d, s, or w key is pressed, and if so act accordingly. Note, that GetKeyDown is only true on the frame that the key was pressed. If you want to check if a key is pressed, disregarding whether it has been pressed for a while or was just pressed, use GetKey instead.

Programmatically Instantiating Objects

Sometimes we need to instantiate different objects programmatically, rather than in the Unity editor. This is especially true for things like bullets, and enemies. Now, there are many different ways to instantiate something, but I will go over the basic methods. You can easily instantiate a primitive object (like a sphere, cube, or plain) or a model.

One way to instantiate your own models, is to create a reference variable (like we did in part 2 with inter script communication) and drag the object you want to instantiate onto your script in the inspector panel. Once you do this, we can do the following


var objectTransform : Transform;//reference variable to model

 

var temp : GameObject = Instantiate(objectTransform, transform.position, Quaternion.identity);

This create whatever object you want to create at the location of the GameObject that has this script. You can of course change the location. The Quaternion.identity part is for the rotation. Rotation is stored at a Quaternion in Unity, and the identity Quaternion is essentially no rotation.

Tracking Points/Cash/Etc.

Tracking points is a fundamental part of most games, but can be somewhat tricky if you have a lot of different way to gain points spread across different scripts. The best and simplest way to keep track of points is to have a single script in charge of keeping track, adding and subtracting points, and setting up methods as interfaces that other functions can use. For example, we could do something like


//point tracking script: PointTracker.js
private var Points = 0;

function AddPoints(x){
 Points += x;
}

function RemovePoints(x){
 Points -= x;
}

 

//usage
if (/*player did something awesome*/){
 var pt : PointTracker = GetComponent(PointTracker);
 pt.AddPoints(50);
}

However, this is more or less the same as if we just used a public variable. Changing the behavior of similar actions that generate the same amount of points would be tedious. However, we could use a system like the following


//point tracking system: PointTracker.js

private var Points = 0
enum PointType{ small = 50, medium = 100, large = 200 }

function AddPoints(type : PointType){
 if (type == PointType.small) Points += 50;
 if (type == PointType.medium) Points += 100;
 if (type == PointType.large) Points += 150;
}
function Remove(type : PointType){
 if (type == PointType.small) Points -= 50;
 if (type == PointType.medium) Points -= 100;
 if (type == PointType.large) Points -= 150;
}

//usage

 

if (/*player did something awesome*/){
 var pt : PointTracker = GetComponent(PointTracker);
 pt.AddPoints(PointType.small);
}
if (/*player did something even more awesome*/){
 var pt : PointTracker = GetComponent(PointTracker);
 pt.AddPoints(PointType.medium);
}

This way, if we want to change the rules for how small point gains work, we only have to change it in 1 place, rather than many!

Basic Timing

Timing in games is very important. Unity provides you with a Timing class called Time, that we can use whenever we need to count something. For example, if you want something to happen every 5 minutes, you would need to do timing. Lets take an example. Say I want to print a message every 5 seconds. I could do


//print a message every 5 seconds
var lastPrint = 0;

function Update() {
if (Time.time - lastPrint > 5){
 print("A message printed every 5 seconds!");
}
}

Another, more practical example may be if I wanted to add something like a bullet delay.

//add 1 sec shooting delay
var lastShoot = 0;

function Update() {
if (Time.time – lastShoot > 1 && if (Input.GetKeyDown(“space”)){
//code to shoot a bullet in game
}
}

[/code]

That ends the four part series in starting Unity. If you have any questions, leave them in the comments below!

Comments
  1. Is it possible to copy and paste news articles for my blog or is that copyright infringement?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s