Guide: HOW TO - Add a new tank
Baroni last edited by
This guide explains how to add a new tank and have it show up in the shop selection screen. The components used in this guide are directed at the multiplayer configuration using Unity Networking (UNET), but can be applied to a project set up for Photon networking in the same way.
Table of Contents
- Problem description
- Creating a new tank
- Creating a new shop product
- Assigning the tank to the network
1. Problem description
You have set up your Tanks Multiplayer project as shown in the 'Getting Started' tutorial on our YouTube channel. After playing with the default project over multiplayer, you would like to customize the project and add your own tanks to it, which the player can buy/use during the game.
2. Creating a new tank
The easiest way to create a new tank is to start with one of the existing tank prefabs as a base template, and swap out the model behind it. So that's what we are going to do in this step. I have created a very simple model I would like to use as a tank already, basically just consisting of several cubes. The important part is that when you are creating your own tank model, make sure that the turret is a separate game object so it can be rotated independently from the bottom of the tank. This is the model I am going to use in this guide.
Next, duplicate one of the existing tanks (e.g. 'TankFree'), located under TanksMultiplayer > Prefabs > Resources and give it a unique name. For this guide I'm going with 'TankSimple'. Note that this duplicate still contains all game objects and scripts like the original prefab at this point.
Drag the prefab into the scene and expand its game object hierarchy. The 'TankRenderers' game object is where the current model renderers are parented to. Since we want to use our own model, you can delete the 'TankRenderers' game object (which will break the prefab connection, but that's fine).
Now, locate your own model, drag it into the scene and make it a child of your current tank template object. Make sure that it is aligned & centered to the parent object nicely, i.e. in the center and above the shadow texture. Also, make sure that the model/prefab you have just added does not have any colliders attached to it. If it does, remove them. The only collider necessary should still be attached to the main object itself.
Locate the turret game object on the tank (or the deepest possible child object attached to it), and create a new empty game object. This new empty object will act as the shot position where bullets are getting spawned from. Name it 'ShotPosition' and move it to where you want it to be, typically at the front of your turret.
Finally, select your model game object and assign it to the 'Player' layer. If you get asked whether or not you want to include child objects, press 'Yes, change children'.
Your model is now set up correctly. But since we've deleted the old model on this tank template, some game object references are missing to the new model. Select the top-level tank game object containing all the important scripts.
You can read about the variable description in our Scripting Reference for the Player script. Assign the missing variables with the game objects from your new model. If necessary, resize the renderers array to fit all renderers that should go in there.
Press 'Apply' in the top right corner in order to save all of your changes back to the new tank prefab, and to complete this chapter.
3. Creating a new shop product
Open the 'Intro' scene. Find the 'Shop' game object (under 'Canvas') and enable it, so we can see the contents of this UI element in the editor. Expand its hierarchy until you can see the product entries. This will be our starting point for this chapter.
Duplicate one of the existing product entries in the scene and give it a unique name, e.g. 'ProductD'. On its IAPProduct script, you should give it a unique identifier and (increasing) selection value as well. For this guide, I'm going to enter Id = 'tank_simple' and Value = '3' (because the last available selection value was 2, so the new tank will have selection value 3).
Note that if you are going to sell the tank in your shop for real money, the Id (identifier) should match with a product identifier you have created on the App Stores respectively. In this example, we'll just make the tank free, so we don't have to worry about any App Store configurations at this point. Uncheck the 'Buyable' checkbox to make the product free.
The product is now configured correctly, but the text is still showing values of the product we duplicated it from. Expand the game object hierarchy until you can see the description, title and icon sprite of this product entry. Feel free to modify these values with something more appropriate to describe your new tank (I'll leave the icon sprite as is).
To complete this chapter, disable the 'Shop' game object again and save the scene.
4. Assigning the tank to the network
In this last chapter, we make the network aware of the new tank selection, which is available in the shop menu, by assigning it to our NetworkManagerCustom component and play-test the game to see it in action.
Open the 'Intro' scene. Select the 'Network' game object, so you can see the NetworkManagerCustom component. In its 'Spawn Info' setting and the 'Registered Spawnable Prefabs' section, click the little '+' to add a new row for your tank. Drag & drop the prefab you've created during this guide from the project panel, Prefabs > Resources folder, into that slot.
You're all set! Start the game and enter the shop - you should be able to select your new tank there (press the Select button). Then, close the shop and enter the game. There you have it - you are playing your new tank in the color of your team!