Time for action - fire!

Let's add a few lines of code to the HeroShip Script to make the bullet fire when you click on the mouse button, because shooting is cool.

function Update ()
{
var halfW:float = Screen.width/2;
transform.position.x = (Input.mousePosition.x )/20;
if(Input.GetMouseButtonDown(0)){
Instantiate(bullet, transform.position + new Vector3(0,3,0), Quaternion.identity);
}
}

Let's review.

if(Input.GetMouseButtonDown(0)){

This line returns true if the left mouse button (which has an ID of 0) has been pushed down within this Update cycle.

Instantiate(bullet, transform.position+new Vector3(0,3,0), Quaternion.identity);

We've used the Instantiate command before this time, we're creating a new BulletPrefab instance in the same position as ...

Get Unity 3D Game Development by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.