Unity小地圖製作(二)

繼續今天的學習心得,unity中小地圖的製作,實現了小地圖中紅色小箭頭代表場景中的主角,然後人物方向的轉變,小地圖中箭頭也隨之改變方向。


右上角就是小地圖,上面有個紅色小箭頭就是代表主角人物所在場景中的位置,箭頭的方向就代表人物所面向的方向。

步驟:

1.俯視圖的製作

首先,我們將Scene場景調成俯視的角度;然後在創建一個Plane,然後點擊該對象,在Inspector屬性窗口將MeshRenderer取消,


並且在場景中會發現有綠色的網格,然後記錄網格所在的位置,並且取消MeshCollider,用截圖工具截取這網格所在的位置,截出一張一模一樣大小的圖片,這就是我們小地圖的來源。截取好了之後記得將MeshCollider勾選上,後面會在代碼中需要計算人物所在的位置,也就正是通過這網格文件來計算的。

2.代碼的編寫

using UnityEngine;
using System.Collections;

public class map : MonoBehaviour
{
    public Texture map1;//小地形圖  
    public Texture jueseTexture;//標識角色的圖片  
    float juesePosX = 0;
    float juesePosY = 0;
    public GameObject player;//角色  
    public GameObject plane;//地形  
    float planeWidth;//地形的寬  
    float planeHeight;//地形的高  
    float angle = 0; //人物旋轉的角度
    void Start()
    {
        //獲取地形的寬高  
        planeWidth = plane.GetComponent<MeshFilter>().mesh.bounds.size.x * plane.transform.localScale.x;
        planeHeight = plane.GetComponent<MeshFilter>().mesh.bounds.size.z * plane.transform.localScale.z;
        print("width+heith:" + planeWidth + ", " + planeHeight);
        print("bounds:" + plane.GetComponent<MeshFilter>().mesh.bounds);
    }
    void OnGUI()
    {
        GUI.DrawTexture(new Rect(Screen.width - map1.width, 0, map1.width, map1.height), map1);

        GUIUtility.RotateAroundPivot(angle, new Vector2((Screen.width - map1.width) + juesePosX + 5, juesePosY + 5));
        GUI.DrawTexture(new Rect((Screen.width - map1.width) + juesePosX, juesePosY, 10, 10), jueseTexture);
    }

    void Update()
    {
        print("people:" + player.transform.position.x + "," + player.transform.position.y);
        print(1);
        //根據palyer在plane的比例關係,映射到對應地圖位置。  
        juesePosX = map1.width * player.transform.position.x / planeWidth + map1.width / 2;
        juesePosY = map1.height * (-player.transform.position.z) / planeHeight + map1.height / 2;

        print("x:" + juesePosX + "y:" + juesePosY);

        angle = player.transform.eulerAngles.y - 90;
        print("angle:" + angle);
    }
}

將該腳本拖放到Plane上,參數說明:JueseTexture是指小地圖中箭頭的圖片,Player是人物模型的Controller,Plane是指當前帶網格的Plane,Map1是指小地圖的圖片。

當然還有一種KGFMapSystem的插件,用來製作小地圖就更炫更專業了,這裏只是一個粗糙的小地圖。你也可以嘗試用一下更專業的插件來開發。


發佈了16 篇原創文章 · 獲贊 16 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章