Unity Space Shooter – Chapter 7 – fire laser shot from player

Shooting shots.

Create an game object called “shotSpawn” as a child of our player.  This will become the origin for the shot when started, but we need a way to communicate between the object and the c# program, so we make a couple helpers

Edit “PlayerController”

Object:

Positon:

Rotation:

We need a way to fill those in.

In the c#

public GameObject shot; // what gets shot

public Transform shotSpawn; // the object to use to get the shot

In Unity

Make an empty game object called ShotSpawn as a child of the Player object. Adjust it to the tip of the nose.  This is where the initial location comes from.

Drag the bullet from the Prefabs into the new “shot” reference

Drag the ShotSpawn into the new shotSpawn reference

 

That is the setup.

 

Now to shoot.

Instantiate the shot when button is clicked (but include a pause) so we dont fire one every frame