In this recipe, we will create a minimap. An ordinary minimap features the basic points of navigation, typically in the shape of a circle or a square. In some instances, a minimap features a scaled-down real-time version of a map. In others, it may be a simplified version of the terrain. Whatever the case may be, minimaps prove to be a useful feature when traversing large landscapes, and they are also a way of indicating to the player the locations of various items. In this recipe, you will start learning how to use render textures and raw images. You will also learn how to set the position of the camera according to the player's position.
Finally, rename it Minimap.
public
variable that stores the player's Transform
, since we will access to the position of this component every frame. In fact, in some games, the player can switch between characters, and this value can change at runtime without us having to modify this script. Keeping this in mind, we will need to add the following to our script:public Transform playerTransform;
Update()
function, we have to assign a new position to our camera. Therefore, we will need to assign a new vector to it, where the x
and z
components, are the same as those of the player, while y
is constant, for the reason explained in step 9. By doing this, we ensure that it will not only follow the player but also be centered on him:void Update () { transform.position = new Vector3 (playerTransform.position.x, transform.position.y, playerTransform.position.z); }
public
variable of our script, and now everything is ready. Let's click on play and test what we have done so far.In this recipe, we saw how to use render textures and raw images to create a minimap.
In fact, we linked the render texture to the camera by setting its target texture
variable. By doing this, the camera has lost its ability to render on-screen. Thus the output of the camera is then stored in Render Texture. When we assign this render texture to Image Raw, it takes the data from the render texture. Finally, it is rendered as a UI element on the screen, which displays what is seen through the camera.
The process is shown in the following diagram:
Now that we have seen how to create a minimap, the following section will teach us how to improve it by setting the camera to work orthographically.
Often, the design of a game requires that the minimap is flat. As far as we have a perspective camera, it will never be as perfectly flat as we would like it to be. Therefore, in this case, we have to change MinimapCamera in such a way that it renders in an orthographic mode rather than a perspective one.
To do this, we need to select MinimapCamera and then change Projection to Orthographic in the Inspector. Now, since Field of View is a parameter that is only for perspective cameras, it disappears. As a result, Size takes its place.
At this moment, the rendering of the camera may appear very close to the terrain. We can try to solve this issue by changing the position value of the y axis. Since it is a top-down view (rotation along the x axis equals 90 degrees, and it is zero otherwise), this doesn't affect the view. This is because all the view rays that are projected are parallel between them. So, we have to change the Size value in order to see more inside our minimap.
Now, we render orthographically and the camera component should look like this:
3.15.12.34