Add the player to the map

Keyboard Shortcuts

In this video, Jesse Freeman will teach you how to create a simple player instance from a prefab and display it in your Unity project. You’ll also learn how to trigger creating the player from a custom Unity Editor button. Finally, the player will be nested inside of the tile map GameObject so it can be moved around the map.

- [Voiceover] Now that we have our player Prefab,let's go ahead and add the player to the scene.In order to do this, we're going to need to modifyour random map tester's code.Let's go down to the script folder and selectthe random map tester class.Let's double click on random map testerand open up the code in MonoDevelop.The first thing we're going to dois add some new fields to this classso that we can store reference to the player Prefaband the instance of the player itself in the game.

Right below where we set the island textures,let's create a new attribute for spaceand one for header.And in the header we'll give it a label called player.Now in the inspector, this will show upas a little bit of a gapbetween the field above itand we'll have a bolded title called playerso that it's easier for us to find the valuesas we're working on them in our game.Now below our header,let's create a new public game object fieldfor the player Prefab.

Next, we'll need a reference to the instance of the Prefabthat we create for our game.Let's create a public game object field called player.Now we'll need a method to create our player.Let's scroll down to after we create our gridand create a new public method called create player.Inside of this method, we're going to create a new playerfrom the Prefab by setting the value of the player fieldto the return value of instantiate player Prefab.

Next we're going to give this game object a nameso that's it's easier for us to find in the hierarchy.We'll simply tell the player fieldthat its name is equal to player.Next, we're going to want to set the player's game objectto be inside of our map containerwhere all of our other tile game objects are.To do this, we're going to need to set the parentof the players transform to be insideof the map containers transform.And finally, we're going to move the playerinto a position that makes it easierfor us to find on the map.

We'll tell the player's transform positionto equal a new vector threeand we'll set the values to zero, zero, zero for now.Now let's save this.And back in Unity,let's go to our random map testerand scroll down to our new player fields.Here, we're going to select the player Prefab fieldand we're going to set it to the night Prefab.

Now we just need a way for us to actually create the player.We're going to add this as a button to the inspector.Inside of our editor folder,we have a random map test editor.Let's double click on this,and we're going to copy the codefor the button that creates the random island.Simply paste it below,and change the label of the button to create player.You'll see that this is setto only run if the application is playing.

And on the script, we're going to change make mapto create player.Let's save this,go back into Unity and run our game.We'll select the random map tester from the hierarchyand scroll down to the bottom.Here, we can generate out our islandand then we can create our player.And now we have our island being createdand a way to add the player into the island.If you look at the random map tester game objectand scroll to the bottom,you'll see the player is now inside the same game objectthat all of our tiles are in.

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.