Player movement class

Keyboard Shortcuts

In this video Jesse Freeman will teach you how to abstract basic 2D map movement into a self contained map movement controller class. This class will handle simple 4-way direction movement. To get started, this class will simply reposition the player based on a tile id index. We’ll also convert the new position values to match up with the map’s tile dimensions.

- [Voiceover] Now it's time for us to lay out the foundation…that'll allow us to move our player around the map.…But thinking ahead, let's try to abstract out…the logic we need to move around the map…into a self-contained class.…We're gonna create a new C# script…called MapMovementController.…Now let's open this up in MonoDevelop.…Here, we can delete the Start and Update methods.…Now let's create a public field…to store a reference to the map itself.…

We'll do public, type it to Map class,…and call it map.…Next, we'll need a vector to store the tile size…so we know how to do the calculations…as we're moving things around in the map.…We'll create a public Vector2 tileSize field.…And finally we'll need two fields…to represent an x- and y- position…we can reuse as we calculate the position.…We'll make this private int tmpX…and private int tmpY.…

Now let's create a new public method…with a return type of void called MoveTo.…Here, we're gonna pass in the index…to the tile we wanna move to.…We'll type it to int and call it index.…

Resume Transcript Auto-Scroll

Author

Released

8/8/2016

Learn how to move a player on a tile-based 2D map and use that movement to trigger events—creating a gaming experience similar to classic RPGs. In this course, Jesse Freeman builds upon the lessons learned in the Unity 5 2D: Random Map Generation: adding a player, moving the player from tile to tile, and making the camera follow the movement, as well as triggering events on the map. Jesse also shows how to add a "fog of war" effect that reveals parts of the map as the player moves around and explores, and how to detect and update the artwork for visited tiles.