[#6] Bullets

Now create an empty game object in the Scene Hierarchy and name it “bullet”. Just like the RainbowCrossbow, we are creating the bullet prefab visually in the Scene Editor to finetune the prefab before putting it into the right controller FSM.

Find the “Teddy grenade” asset in the FPL_ISBIT’s Weapons Pack folder under the 3D section, and drag it into the bullet object to make it a child. It should now appear on screen in the Scene Editor as follows:

Note that bulletFinished is shown here in the hierarchy as this was used as a reference to make the tutorial. You should not expect to see this of playmakerGameFinished in your own Scene Hierarchy.

 

If the bear is off-center, recenter it by clicking the gear icon under the Transform component in the Inspector and select “reset”. Next select the bullet and in the Inspector window select Add Component and then search for Capsule Collider. Once you have found it, click on it to add this component to the bullet.

Edit the shape of the collider so that it roughly encapsulates the bear. Click Edit Collider to enable click-and-drag editing, or manipulate the collider by directly changing its radius and height.

Also make sure to give the bullet a Rigid Body so that the game world’s physics will apply to it. Also ensure that Use Gravity is checked so that the bullet will drop as it flies, just like a bullet in the real world.

Now create a collider on TeddyGrenade as well, but make it slightly bigger than the collider you placed on the bullet. This is because the TeddyGrenade’s collider will be a trigger that can be used to trigger certain actions on contact with the enemies, however being a trigger stops an asset from colliding normally with other objects such as the floor. Thus we want a trigger zone that is larger than the actual collider of the bullet so the trigger zone overlaps with the enemy models when they collide before the bullet bounces off. The bullet still needs to have a proper collider that is not a trigger to allow it to bounce off enemies and the floor.

To make the TeddyGrenade’s collider a trigger, check the “Is Trigger” box on the TeddyGrenade’s collider properties screen in the Inspector.

Click on the bullet Game Object and add an FSM. Then create a state called “Moving Forward”.

 

As you can see in the Scene Hierarchy here, the red square logo to the right of an asset’s name indicates that it has an FSM attached to it.

 

In the Moving Forward state create an Add Force action, and set the force to have a value of 4000 in the Z direction. Leave all the other axes alone. This will cause the bear bullet to fly off directly away from the gun, as one would expect when firing a bullet. Make sure to set the Space to Self to guarantee that the Z direction is the one relative to the bullet and not the game world itself, since the bullet’s and not the world’s Z direction is the one that points directly away from the main axis of the crossbow.

Now create an event called “die” and add it as a transition to the Moving Forward state. Then add a Wait action to the state and set it to 20 seconds in real time by checking the corresponding box. Make sure to set the finish even to “die” as well. This will cause the bullet to enter the die state where it will be destroyed after 20 seconds, which is necessary to stop the screen from becoming cluttered with bullets, which would cause the game to lag severely over time.

In order to ensure the bullet’s destruction create a state called “Dying” and connect it with a transition arrow from the “die” state. Then, add a Destroy Self action to the Dying state from the action browser. This action destroys the bullet entirely and removes it from the scene. We want to keep Detach Children unchecked so that the TeddyGrenade asset will also be deleted along with the bullet, since it will still be attached to it when the bullet gets destroyed.

Now, drag the bullet into the myPrefabs folder to make it into a prefab.

Return to the Spawning Bullet state in the gunController_R Game Object’s FSM and drag the new bullet prefab into the Game Object field of the Create Object action that we left unfinished earlier on. It should have a little red warning mark next to it so should be easy to identify quickly.

We can also delete the bullet prefab from the Scene Hiearchy at this point, since we will no longer need to edit it visually in the Unity Scene Editor.