Adding movement to the hero
Just like in the previous chapter, we will now add movement to our hero. He'll walk over the ground plane and turn around when he reaches the edge of the screen.
The Hero3D class
We'll start by creating a Hero3D
class that will encapsulate all the behavior for movement. This class will inherit from GameObject3D
class, and just like Hero2D
, we will use composition.
Fields
The class has three fields, the model, the direction in which the player will move, and the walk speed.
private GameModel _heroModel; private int _direction = 1; //1 = Right / -1 = Left private const int Speed = 75;
Initialize
In the Initialize
method we will create a new GameModel
instance and set its position.
_heroModel = new GameModel("Game3D/Vampire"); _heroModel.Position = new Vector3(0, -147, -100);
LoadContent and Draw
In the LoadContent
and Draw
methods we just call LoadContent
and Draw
methods on _heroModel
.
Update
The update is where all the magic happens. In this method, we will calculate the...