Introduction
This article demonstrates how to make a Game Minimap using C# scripts in unity.
Prerequisites
Unity environment version 2019.3.0f6.
Create the Project
First, you have to open the Unity project. Create the plane.
Rename the plane as floor in scene view.
Click on the "GameObject" menu in the menu bar. Select 3D objects and pick the "Capsule" option. The capsule will be added to the scene View.
Rename the Capsule as Player in Scene view.
Click on the "GameObject" menu in the menu bar. Select 3D objects and pick the "Cylinder" option. The cylinder will be added to the scene View
Select the Cylinder on Click (Ctrl + D). Create a duplicate multiple Cylinder.
Create a C# Script
Right-click on Assets. Select Create >> C# script.
Rename the script as MoveScripts.
Double click on the MoveScript script. The MonoDevelop-Unity editor will open up.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MoveScript : MonoBehaviour
- {
- [SerializeField] private float speed;
- void Update()
- {
- Vector3 keyboardInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
- transform.Translate(keyboardInput * speed * Time.deltaTime);
- }
- }
Save the Program.
Drag and drop the MoveScripts onto the Player.
Set the Player Speed in the Scene View.
Add the new camera in Scene View.
Now click Camera and set the Transform & Projection in Scene View.
Rename the Camera as MinimapCamera.
Click on the "GameObject" menu in the menu bar. Select UI objects and pick the "Canvas" option. The canvas will be added to the scene View.
Set the canvas UI Scale Mode In Scene View.
Select Canvas add the Panel in Scene View.
Set the Panel Rect Transform in Scene View.
Rename the Panel as Minimap Background.
Select MinimapBackground and add the Image in Scene View.
Rename the Image as MinimapRander.
Create a Render Texture
Right-click on Assets. Select Create >> Render Texture.
Rename the Render Texture as MinimapCamera.
Select MinimapRender Add Component Click on Raw Image in Scene View.
Drag N Drop MinimapCamera to MinimapRender RawImage.
Drag N Drop MinimapCamera to MinimapCamera ( Target Texture ).
Create a C# Script
Right-click on Assets. Select Create >> C# script.
Rename the scripts as MinimapCamera.
Double click on the MinimapCamera script. The MonoDevelop-Unity editor will open up.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MinimapCamera : MonoBehaviour
- {
- [SerializeField] private Transform PlayerTransform;
- [SerializeField] private float yoffset;
- private void LateUpdate()
- {
- Vector3 targetPosition = PlayerTransform.position;
- targetPosition.y += yoffset;
- transform.position = targetPosition;
- }
- }
Save the Program.
Drag and drop the MinimapCamera Scripts onto the MinimapCamera.
Drag and drop the Player onto the Minimap Camera Scripts (Player Transform and Set the Offset).
Click on the "Edit" menu in the menu bar. Select Player Setting objects and pick the "Tag and Layers" option. Click on layers create the user Layers (Enemies) to the scene View.
Click on the "Edit" menu in the menu bar. Select Player Setting objects and pick the "Tag and Layers" option. Click on layers create the user Layers (RenderToMinimap) to the scene View.
Select the Cylinder and add the user layers (Enemies).
Select the All Cylinder Add to sphere in scene View.
Select the Sphere Add the user Layers ( RenderToMinimap ).
Select the MinimapCamera and click On Culling Mask. Set the RenderToMinimap.
Create The Materials
Drag n drop materials to Cylinder in Scene View.
Drag N Drop materials to Sphere in Scene View.
Click on the Play button. Start the Game Minimap in Unity.
Summary
I hope you understood how to perform Game Minimap using C# scripts in unity.