O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required