Game Development Reference
In-Depth Information
Notice that we save on extra code and variables by simply checking the horizontal
scale of the
Player
object to determine which direction they are facing. Handy!
Now save this as a Prefab called
Player Bullet
and delete the current instance of
it from the game world. We won't need that one.
Next, we want to add a new state to the player called
firingWeapon
. Open up
PlayerStateController
and add the new state to the
playerStates
enum. Simply
add it to the end of the enum, as follows:
kill,
resurrect,
firingWeapon, // Our new state!
_stateCount
There is one minor complexity in adding this state: we want the
firingWeapon
state
to immediately switch back to the previous state. To do this, let's add the ability to
store what the previous state was.
At the beginning of
PlayerStateListener
, add the following line of code:
private PlayerStateController.playerStates previousState =
PlayerStateController.playerStates.idle;
Next, add support for the code to store the previous state whenever it is changed.
At the bottom part of
onStateChange
in
PlayerStateListener
, modify the code
so that it looks like the following:
//Store the current state as the previous state
previousState = currentState;
Finally, assign the new state to the player object:
currentState = newState;
The previous state is now properly stored when we change states—fantastic! Now
we are ready to set up the code for the
firingWeapon
state. At the beginning of
PlayerStateListener
, add the following additional code:
public GameObject bulletPrefab = null;
Be sure to also add the
Player Bullet
Prefab to the Bullet Prefab slot in the
Player
object to fill this new property!