Unity3D game development-Implementation of small maps

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.