In this video Jesse Freeman shows you how to start and stop the player walk animation. You’ll leverage existing code for telling when the player has reached a new tile as well as adding a new delegate to capture when the player starts moving. Once you know what the player starts and stops moving you’ll be able to manipulate the player’s walk animation speed accordingly.
- [Voiceover] So now that we know when the player stops moving, let's add an event for when the player begins his movement. We'll need to open the map movement controller and scroll up to the top. Here, we can copy the delegate that we created before, and paste it below. In this case, let's rename it from TileAction to MoveAction. We'll also want to rename the callback to MoveActionCallback.
The last thing we can do is delete the type that gets passed in. In this case, the MoveAction isn't going to return any information, since it's only being triggered right as the player begins his movement. Well, let's scroll down to our MoveTo method. Right after we test if the player can move, let's go ahead and call the MoveAction. Here, we'll test to see if the MoveActionCallback does not equal no. If it's been set, we'll call the MoveActionCallback.
Well, let's go over to our player for a minute. One of the things that we can do, by now detecting when the player has begun moving and when he has stopped moving, is actually start and stop the animation. So at the top of our class we're going to need to create a new private field to store reference to the animator. Here we'll make a private type Animator, and let's call this field Animator.
Below our start method, we're going to want to create two new methods to handle our callbacks. The first will be a private method called OnMove. This will get triggered when the player begins his movement. In this case, we're going to tell the animator speed to equal one. Below this, we're going to create a new method called OnTile, and in this we're going to pass in an int type. In this case, we're going to tell the animator speed to be set to zero.
Now in the start method, below where we set up our moveController, let's add the file in. On the moveController, we're going to add a MoveActionCallback, and connect to our OnMove method. Next on the moveController, we're going to connect the TileActionCallback to the OnTile method, and finally, we're going to need to get a reference to the animator itself. We'll set the animator by calling GetComponent, and passing in the Animator class to the method.
Finally, we're going to start up our player. Let's set the animator speed to equal zero. Now, let's save our code, and go back into Unity. Here, we'll now see the player has stopped moving. If we move, you'll see the animation happens as well, and now we have a fully animated player. One thing we can do, is stop the game, go into our Prefabs, select the knight, and scroll down to our move script.
Here you can see that our speed is set to one. Let's go ahead and make the player move a little bit faster, by changing the speed value to point five. Also, if you go into the console, you'll see that we still have the callback tracing out for which tile type we're on. We don't need this for now. Let's click on the output in the console window, which will take us to our random map tester class. Here, we can simply comment out our Debug.Log statement.
Let's save this, and run the game. Now, the player will move a lot faster, and you'll see the animation. At this point, all of the player movement logic is in place. We're able to start and stop the animations, we're able to call a trigger whenever we land on a specific tile, and we can tell that tile type, as well as having the camera move and follow the player as we walk around the map. The only thing left to do is to add a little bit more polish to our map.
- Setting up the project
- Adding and repositioning the player in the map
- Listening to movement
- Moving the player
- Triggering events from movement
- Adding fog
- Tracking visited tiles
- Expanding the player's field of view