繼續今天的學習心得,unity中小地圖的製作,實現了小地圖中紅色小箭頭代表場景中的主角,然後人物方向的轉變,小地圖中箭頭也隨之改變方向。
右上角就是小地圖,上面有個紅色小箭頭就是代表主角人物所在場景中的位置,箭頭的方向就代表人物所面向的方向。
步驟:
1.俯視圖的製作
首先,我們將Scene場景調成俯視的角度;然後在創建一個Plane,然後點擊該對象,在Inspector屬性窗口將MeshRenderer取消,
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);
}
}
當然還有一種KGFMapSystem的插件,用來製作小地圖就更炫更專業了,這裏只是一個粗糙的小地圖。你也可以嘗試用一下更專業的插件來開發。