Game Development Reference
In-Depth Information
}
//is the player pressing left button?
if(Input.GetKey("left"))
{
//ship moves left
transform.Translate(Vector3(-2,0,0));
}
//is player pressing the fire button
(spacebar)
if(Input.GetKeyDown("space")&&canShoot)
{
//create the bullet
Instantiate(myBullet,transform.position,transform.rotation);
//player can't fire for a while
canShoot=false;
}
}
7. Save the file and go back to Unity, then, in the
Project
panel, drag the script
onto the
PLShip
prefab to add it.
8. You can check in the
Inspector
panel that the script has been added to the
prefab. You will also notice that the script requires a
Rigidbody
variable
to be instantiated for the script to work, as defined by this line of the
Con-
trolShip
script:
//this var is needed to fire bullets from
the ship
var myBullet:Rigidbody;
9. To instantiate the variable, we first need to add the
Rigidbody
component to
the bullet. To do that, select the
PLBullet
prefab in the
Project
panel, then go
to the main menu and select
Component
|
Physics
|
Rigidbody
, as shown
in the following screenshot: