Game Development Reference
In-Depth Information
4. As you can see, any newly created JavaScript file is already provided with
two main function declarations: the Start() function and Update() func-
tion.
5. The Start() function is useful to set default values for variables when the
game starts, while the Update() function is a very important one, which is
called by Unity engine (almost) once per frame. Basically, when you need
some operation to be performed continuously, put your code inside the Up-
date() function.
6. To take control of the player ship we need the following code to be added to
the script. We put comments to make the operations performed clearer, as
we cannot make a full explanation on game programming here.
#pragma strict
//this var is needed to fire bullets from
the ship
var myBullet:Rigidbody;
//this is a true\false var to control
player's ship fire
//rate
static var canShoot:boolean;
function Start () {
//we want the player to be able to
shoot as the game
//starts
canShoot=true;
}
function Update () {
//is the player pressing right button?
if(Input.GetKey("right"))
{
//ship moves right
transform.Translate(Vector3(2,0,0));
Search WWH ::




Custom Search