Game Development Reference
In-Depth Information
1. Create a new JavaScript in the
Scripts
folder, name it
ControlPLBullet
and open it in
MonoDevelop
.
2. This script contains instructions for the
Update()
function and the
OnTrig-
gerEnter()
function to check for collisions between the bullet and the ali-
ens, their bullets and the barriers positioned between the player's ship and
the swarm.
3. The
Update()
function takes care of making the bullet move up and eventu-
ally destroys it in case nothing is hit, so that the player can shoot again. The
following code is to be typed into the
Update()
function:
function Update () {
//move bullet up once created 2
pixels\frame
transform.Translate(Vector3(0,2,0));
//Y=100 defines upper screen limit
if(transform.position.y>100)
{
//destroy bullet as it goes
outside the upper
//screen limit
Destroy(gameObject);
//once the bullet is destroyed,
allow the player
//to shoot again
ControlShip.canShoot=true;
}
}
4. In the
OnTriggerEnter()
function we check what the bullet collides with
and then destroy both:
function OnTriggerEnter(other:Collider)
{
//the bullet collides with aliens
if(other.gameObject.tag=="Enemies"){