繼續今天的學習心得,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的插件,用來制作小地圖就更炫更專業了,這里只是一個粗糙的小地圖。你也可以嘗試用一下更專業的插件來開發。
==================== 迂者 丁小未 CSDN博客專欄=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
Unity QQ群:858550 cocos2dx QQ群:280818155
====================== 相互學習,共同進步 ===================
轉載請注明出處:http://blog.csdn.net/dingxiaowei2013/article/details/18571083
