Bullets and Firing
Now that we have directions and aiming, we can start working on implementing the shooting.
Bullet Setup
Before we add functionality to shoot, we need to first create the bullets Samus will be shooting.
First create a new Game Object named Bullet.
Give it a SpriteRenderer component with Sprite: metroid_samusaran_sheet_2.
Give it a SphereCollider component with IsTrigger: True and Radius: 0.2
Give it a Rigidbody compontent with Use Gravity: Off and the same Constraints as the Rigidbody on Player.
Now let’s make it a Prefab by dragging it from the Inspector to our Prefabs folder. Delete the instance from the scene.
Firing
Now we need to let the player shoot the Bullet prefab on a button press. Create a new script PlayerWeapon.cs in your Assets/Scripts folder and add it to your Standing Game Object (the child of Player).
Now let’s set up some of our variables we’ll need.
We need to store a reference to our PlayerDirection.
We need a public GameObject for our bulletPrefab.
We need two public Transforms. One for firingPositionForward and one for firingPositionUpward;
We need a public float for the firingSpeed. Set this to 10 for now.
We now have four public variables that we can set in the inspector and one implicitly private variable that still needs to be setup. Let’s setup that private variable (playerDirection) now in Awake. Note that we need to use GetComponentInParent() instead of GetComponent() as the PlayerDirection script is attached to Player, not Standing
Before we can set the other variables, we need to actually create the firing positions. Create two new empty Game Objects, Firing Position Forward and Firing Position Upward.
Set both as children of the Standing.
Now set the local position of both to (0.6, .365, 0) and (.125, 1.25, 0) respectively.
(Gray circles added for visualization)
Now that we have our firing positions, we can add references to their Transform in the inspector.
Let’s also add a reference to the Bullet prefab we created.
We don’t need to do anything with firingSpeed right now as we already set that to 10 in the script.
Now that our references are set up, we can start writing the actual firing code.
This code will be pretty simple: in an Update check if Input is down for the S key. When the button is pressed create a new instance of the Bullet prefab.
Set the position and velocity correctly depending on the playerDirection
And that’s it, our player is now firing… infinitely lasting bullets.
Destroying Bullets
If you run your game you’ll notice that our Bullets never get Destroyed; they exist forever. There are two ways that bullets should be destroyed, so let’s create two scripts, one for each case.
The first reason a Bullet should be Destroyed is after a short period of time. Create a new script DestroyOnTime.cs and add it to the Bullet prefab.
As the name suggests, all we need to do here is Destroy the object after a some time. We can easily do this by supplying an optional parameter to Destroy().
In the Inspector, let’s give the public destroyTime variable a value of .35
The other reason a Bullet should be Destroyed is when it hits something. Create another new script DestroyOnTriggerEnter.cs and add it to the Bullet prefab. Again, this script is pretty simple: it should Destroy() the object OnTriggerEnter().
If you play the game you’ll notice something interesting: shooting up works fine but shooting forward doesn’t appear to do anything.
This is because the Bullet instance is colliding with the Standing Capusle Collider and immediately destroying itself. This is no good, so let’s fix it by telling Unity not to check for collisions between Bullets and the Player.
We can do this through the Collisison Matrix, but first we need to add Layers to both objects.
Open Tags & Layer Manager by going to Edit->Project Settings->Tags & Layers . Create two new Layers: Player and Bullet.
Now we need to add these to the correct prefabs.
Set the Layer of the Bullet Prefab to Bullet.
Set the layer of the Player Prefab to Player. When it asks if you want to apply this for all children, say Yes, change children as the collider is actually part of the Standing child object.
Now we can update the Collision Matrix. Go to Edit->Project Settings->Physics and in the Collision Matrix disable collision between Player and Bullet.
Also disable collision between Bullet and Bullet, so that shooting two bullets quickly won’t cause them to Destroy each other.
Conclusion
Great, our Player now has the ability to shoot! Of course, these bullets don’t do anything yet, it will be up to you to add damage when you start adding enemies.
When you’re ready, commit your changes and move on to 10: Audio.