Creating an enemy

Now that we can take pictures, let's create an enemy to take pictures of! We will perform the following steps:

  1. The first step to adding in an enemy is to import the assets required to use it. Inside our Chapter 9Art AssetsGhost Model location, you'll find a series of files. Go ahead and drag and drop the folder into your project.
  2. Open up the Ghost Model object from the Project tab and then select the Ghost_mesh object. Now, select the object to bring up its properties in the Inspector tab. Under Scale Factor in the Model tab, change the value to .10 to scale the object to 1/10th of its starting size. Then, check Generate Colliders, making sure to hit Apply.
  3. Under the Rig tab, confirm the Animation Type value to None, as this model doesn't have any animations and then click on Apply if needed.
  4. Now, go somewhere in your world in the Scene view, and drag and drop the character onto the screen near your player and terrain. Take a look at the following screenshot:
    Creating an enemy

Now, save the project, level and start up the game.

Creating an enemy

And now, the character is displayed and we can see them in the game. Spooky!

State machines 101

We oftentimes write code to provide the reactive or interactive parts within our simulation (or game world)—things such as when you're pressing a button or if you're walking or jumping. If you look at real life, you should note that a lot of things are reactive systems in that same way, such as your mobile phone and toaster. Depending on the stimuli provided to these objects, the state of these objects may change. We describe something that can be in one of the multiple states at a time as a state machine.

Almost every program that we write is or can be a state machine of some sort because technically, the moment you write an if statement, you've created code that can be in one of at least two states. However, having a number of switch and if statements can quickly get out of hand, making it very hard for people to understand what your code is actually doing. As a programmer, we want to isolate problems and break them down into their simplest parts before jumping in to solve them.

There are different kinds of state machines, but for this first example, we are going to create a simple Finite State Machine (FSM). When I say finite, it means that each of the states is already defined ahead of time. With a finite state machine, we can have different states in which we can process input differently depending on the state.

For example, if you are on a ladder, you can only move up and down and not to the sides.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset