From the course: Unity 5: 2D Movement in an RPG Game

Limit movement - Unity Tutorial

From the course: Unity 5: 2D Movement in an RPG Game

Start my 1-month free trial

Limit movement

- [Voiceover] Now that we got our player moving, it's time for us to look into ways that we can limit the way that the player moves. One thing that's showing up when we run our game is that if you continue to hit the Move key, you'll see that the player moves around the map in weird ways. That's because he's not snapping to the grid. Also, you can move over tiles that you shouldn't be able to walk on, like mountains and the ocean. Let's take a look at how we can limit the movement of the player. We'll open up the Map Movement Controller. At the bottom of this class, let's add a new private method that will return a boolean called CanMove, and in it we're going to paste in an int for the index of the tile we want to test that we can move to. By default, we want to return true, so that the player can move on any tile. Now let's create a condition that tests to see if the player is moving. If this is true, we're going to want to return false so that we only accept movement when the player is stopped. To make this work, we'll need to scroll up to the MoveTo method, and at the top before we do anything, we're going to want to do a condition to test if CanMove returns false. We'll paste in the index, and if this is false we'll simply return out of this method. Now let's save our code and go back into Unity. If we run the game and try to move, you'll see that we can only move when the player stops. Now let's take a look at how to limit the way that the player moves on specific tiles. Let's go back into our Map Movement Controller. Scroll down to the CanMove method and above our condition to testing if the player is moving, let's create a new variable for tileType. Here we'll look at the map's tiles array and pull up the index of the tile that we're looking for, and we'll set the value to that tile's auto tile ID. We can look to see what this value is by simply outputting it to the Console window. Here we'll do debug.log, and we'll paste in tyleType. If we run the game, the first tile we'll see will equal 20, which is the castle. If we move down you'll see mountains are 18, and if we move around to the ocean you'll see that the ocean is negative one. We're going to want to make sure that the player isn't able to walk on ocean or mountain tiles, so let's stop this and go back into our Map Movement Controller. At the top of our class, we're going to create a new public field that will be typed as an int array and we'll call this blockedTileTypes, and we'll store values inside of this array that we can test against to see which tile type the player is on, and whether it exists in this array. In order to do this, we're going to need to import the array class, so at the top of our class let's type in using System to have access to the array utilities. Let's scroll down to our CanMove and delete the debug.log statement, and in the moving condition we'll create an or to test whether the array.IndexOf blockedTileTypes contains the tile type value we set earlier. We want to make sure that the return value of the index of method is greater than negative one, and then we can save this. Now let's go back into our project and select Knight PreFab. If we scroll down to our Map Movement Controller script, you'll see we now have an array of blocked tile types. Let's change the size to two, and make the first element negative one and the second element 18. Now let's re-run the game. Let's generate out a map so that our castle is close to a mountain and ocean. If I move to the left, you'll see I'm now blocked by the mountain, but I can still move up. But if I try to move up, I'm blocked as well by the ocean. Now we can control which tiles the player can move onto, and which ones they can't.

Contents