Today, let's talk about how to implement small maps in Unity3D. First, let's take a look at the final effect:
To achieve the effect of a small map, you need to plot the small map and the role. Therefore, you need to use the OnGUI () method. Let's look at the Code:
Using UnityEngine; using System. collections; public class camerasur: MonoBehaviour {// defines the map public Texture MapTexture; // defines the role flag map public Texture PlayerTexture; // defines the third-person role controller private GameObject m3rdPersonControl; public GameObject BulletPos; public GameObject Bullet; // define the role position private float mPersonX; private float mPersonZ; // define the scaling ratio private float mScale; void Start () {// obtain the role controller m3rdPersonControl = GameObject. F Ind ("3rd Person Controller"); // obtain the role location mPersonX = m3rdPersonControl. transform. position. x; mPersonZ = m3rdPersonControl. transform. position. z; // calculate the zoom ratio mScale = (200F/Screen. width) <(200F/Screen. height )? (200F/Screen. width) :( 200F/Screen. height);} void Update () {// refresh the role location mPersonX = m3rdPersonControl. transform. position. x; mPersonZ = m3rdPersonControl. transform. position. z; if (Input. getMouseButtonDown (0) {Vector3 Target = Camera. main. screenToWorldPoint (new Vector3 (Input. mousePosition. x, Input. mousePosition. y, 1); Vector3 Dir = Target-m3rdPersonControl.transform.position; Instantiate (Bullet, BulletPos. transform. position, Quaternion. identity) ;}} void OnGUI () {// in the upper-right corner of the screen, draw a map GUI of x200. drawTexture (new Rect (Screen. width-200, 0,200,200), MapTexture); // uses the right hand of the origin in the lower left corner of the map. The width and height of the role are 20 guis. drawTexture (new Rect (Screen. width-200 + mPersonX * mScale + 20,200-mPersonZ * mScale-20, 20, 20), PlayerTexture );}}
In the above Code, we set the width and height of the small map to 200, set the width and height of the role identifier to 20, and set the map to the north direction. In the initialization method Start (), we obtain a role controller to obtain the role location and calculate the scaling ratio based on the small map and the screen. In the OnGUI () method, we draw small maps and role identifiers Based on the scaling ratio. In the Update () method, we refresh the role location. This is the content of today. I hope you will like it!