Time for action – fire!
In order to have something for the ship to fire, we have to create a variable at the top of the script called bullet
, and then click-and-drag the Bullet Prefab into the variable slot in the Inspector panel. Otherwise, the script will have no idea what we're talking about when we try to instantiate something called bullet
.
Follow these steps:
Add the variable declaration at the top of the HeroShip Script:
var bullet:GameObject;
Save the script.
In the Hierarchy panel, select the HeroShip Prefab.
In the Inspector panel, find the HeroShip Script component.
Hook up the Bullet Prefab to the
bullet
variable.
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 () { transform.position.x = (Input.mousePosition.x )/20; if(Input.GetMouseButtonDown(0)){ Instantiate(bullet, transform.position + new Vector3(-3,2,0), Quaternion.identity); Instantiate(bullet,...