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

Add the player to the map - Unity Tutorial

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

Start my 1-month free trial

Add the player to 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 modify our random map tester's code. Let's go down to the script folder and select the random map tester class. Let's double click on random map tester and open up the code in MonoDevelop. The first thing we're going to do is add some new fields to this class so that we can store reference to the player Prefab and the instance of the player itself in the game. Right below where we set the island textures, let's create a new attribute for space and one for header. And in the header we'll give it a label called player. Now in the inspector, this will show up as a little bit of a gap between the field above it and we'll have a bolded title called player so that it's easier for us to find the values as we're working on them in our game. Now below our header, let's create a new public game object field for the player Prefab. Next, we'll need a reference to the instance of the Prefab that 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 grid and create a new public method called create player. Inside of this method, we're going to create a new player from the Prefab by setting the value of the player field to the return value of instantiate player Prefab. Next we're going to give this game object a name so that's it's easier for us to find in the hierarchy. We'll simply tell the player field that its name is equal to player. Next, we're going to want to set the player's game object to be inside of our map container where all of our other tile game objects are. To do this, we're going to need to set the parent of the players transform to be inside of the map containers transform. And finally, we're going to move the player into a position that makes it easier for us to find on the map. We'll tell the player's transform position to equal a new vector three and 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 tester and scroll down to our new player fields. Here, we're going to select the player Prefab field and 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 code for 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 set to only run if the application is playing. And on the script, we're going to change make map to create player. Let's save this, go back into Unity and run our game. We'll select the random map tester from the hierarchy and scroll down to the bottom. Here, we can generate out our island and then we can create our player. And now we have our island being created and a way to add the player into the island. If you look at the random map tester game object and scroll to the bottom, you'll see the player is now inside the same game object that all of our tiles are in.

Contents