Unity RTS Tutorial Part 3 - Terrain
Problems Adding Terrain
I need to add terrain here because in the next part I will be adding a mini map. One of the elements of that part is to add a lower quality copy of the terrain to the mini map rather than the main terrain so that a fully detailed terrain isn't being rendered twice.
I thought adding terrain should be no problem here. I have done it before as part of another tutorial, although I cannot say I am experienced. I added a new terrain game object and tried to use the tools to "paint" my terrain. Nothing happened. After a bit of persistent attempts I turned to Google. It turns out there has been a bug in Unity for a while and the terrain tools do not work if you have changed the editor layout as I have. Apparently setting the layout to default fixes the issue. It didn't work for me so it may not have been a problem. I later found out that my problem was the "Opacity". If this is set to 0 no terrain will appear as you click on the terrain in the scene view. I went through a few terrain tutorials and some of the Unity manual on Terrain and at no point did I come across anything saying the "Opacity" needed to be adjusted before beginning. It is also worth mentioning that when you get to painting testures that the "Target Strength" may also default to 0 and needs to be above 0 for the tool to work.
Adding the Terrain
Finally we get on to actually adding the terrain. There are plenty of good tutorials on adding terrain so I won't be going into any detail about how to use the tools here. I just want to add a terrain to work with and show how it is integrated into our game. You can experiment with making adjustments to your own whenever you want.
In the Map scene we need to add a new terrain game object. It is found under the 3D object menu. Unity adds the game object to your scene and create a new terrain map file in your asset foler. I renamed mine "Map" and moved it into the scenes folder. I renamed my terrain object in the Scene to be "Ground". I then set my terrain to match the size of my current Ground Object. For me it has a transform position of x-550,y0,z-500. If you select the terrain game object you will see the menu above. The button on the right opens the terrain setting and will allow you to alter the length and width. mine are both currently set to 1000. You can then use the terrain paint tools to create a landscape. I also used the smoothing tool to make my terrain a hilly, but not mountainous landscape.
With this done you can remove the previous ground game object and remove or rename its prefab. Create a prefab of the new terrain object and replace the previous ground in the "GameObjectList". I am not 100% sure the terrain will need to be a prefab or in GameObjectList, but with the code as it is at the moment it is required. If you run the game now your units should walk on the terrain.
Building on Top of Terrain
If you gave the terrain a good test you will notice that the buildings still get build at y=0. In many cases that even means that the building is placed below the surface of the terrain. To fix this we need to update the "FindBuildingLocation()" method in "Player.cs". This method gets the position the mouse is clicked when hovering over the terrain with the temporary building. The Following line needs to be removed or commented out;
newLocation.y = 0;
Your game should now run and allow the buildings to be created on top of terrain. You will notice that if you build the building on a slop that there is empty space under the building on the downhill side of it. I plan to add foundations to my buildings when I add more buildings and units later, but for the moment I will live with it. Please see link below to the project on GitHub.