Game Development Reference
In-Depth Information
With that, the player can now fire its weapon! Play the game and press the Fire1
button (by default, it's the Ctrl key) and then watch a consistent stream of bullets fire
out from the player! However, the player currently fires a burst of bullets on every
firing. So, let's add in a quick firing delay. To do this, we will simply make use of our
state abort check.
In PlayerStateListener , add the following line of code in the Start method:
if(PlayerStateController.stateDelayTimer[
(int)PlayerStateController.playerStates.firingWeapon] = 1.0f;
Next, go into checkIfAbortOnStateCondition at the bottom part of
PlayerStateListener and add the following code in the firingWeapon check:
if(PlayerStateController.stateDelayTimer[
(int)PlayerStateController.playerStates.firingWeapon] >Time.time)
returnVal = true;
Finally, go into the onStateChange method in the same script file and add the
following line of code to the firingWeapon state in the newState switch condition:
PlayerStateController.stateDelayTimer[(int)PlayerStateController.
playerStates.firingWeapon] = Time.time + 0.25f;
This will cause a delay of 0.25 seconds (about a quarter of a second) between each
shot. Now, bullets have a firing delay to prevent them from streaming out as fast
while the player holds down the button. Also, this prevents bursts from occurring
when the player just taps the firing key.
At this point, you may want to tweak the physics property of the bullet so it flies
faster or differently. Try playing with the Mass field of the bullet's Rigidbody
2D component.
Bullets are better when they hit things
OK! Almost there. Let's do one more thing. Let's set the bullet to destroy itself
whenever it collides with something that's different from itself, or after a preset
time. Let's start by having the bullet auto-destroyed after a preset time.
Open up PlayerBulletController and add the following line of code at the
beginning, just after the bulletSpeed definition:
private float selfDestructTimer = 0.0f;
 
Search WWH ::




Custom Search