public class smallMap : MonoBehaviour {
//大地圖地形物件
GameObject plane;
//大地圖主角物件
GameObject cube;
//大地圖的寬度
float mapWidth;
//大地圖的高度
float mapHeight;
//地圖邊界的檢測數值
float widthCheck;
float heightCheck;
//小地圖主角的位置
float mapcube_x = 0;
float mapcube_y = 0;
//GUI按鈕是否被按下
bool keyUp;
bool keyDown;
bool keyLeft;
bool keyRight;
//小地圖的背景貼圖
public Texture map;
//小地圖的主角貼圖
public Texture map_cube;
void Start()
{
//得到大地圖物件
plane = GameObject.Find("Plane");
//得到大地圖主角物件
cube = GameObject.Find("Cube");
//得到大地圖默認寬度
float size_x = plane.GetComponent<MeshFilter>().mesh.bounds.size.x;
//得到大地圖寬度的縮放比例
float scal_x = plane.transform.localScale.x;
//得到大地圖默認高度
float size_z = plane.GetComponent<MeshFilter>().mesh.bounds.size.z;
//得到大地圖高度縮放地理
float scal_z = plane.transform.localScale.z;
//原始寬度乘以縮放比例計算出真實寬度
mapWidth = size_x * scal_x;
mapHeight = size_z * scal_z;
//越界監測的寬度
widthCheck = mapWidth / 2;
heightCheck = mapHeight / 2;
check();
}
void OnGUI()
{
keyUp = GUILayout.RepeatButton("向前移動");
keyDown = GUILayout.RepeatButton("向后移動");
keyLeft = GUILayout.RepeatButton("向左移動");
keyRight = GUILayout.RepeatButton("向右移動");
//繪制小地圖背景
GUI.DrawTexture(new Rect(Screen.width - map.width, 0, map.width, map.height), map);
//繪制小地圖上的“主角”
GUI.DrawTexture(new Rect(mapcube_x, mapcube_y, map_cube.width, map_cube.height), map_cube);
}
void FixedUpdate()
{
if (keyUp)
{
//向前移動
cube.transform.Translate(Vector3.forward * Time.deltaTime * 5);
check();
}
if (keyDown)
{
//向后移動
cube.transform.Translate(-Vector3.forward * Time.deltaTime * 5);
check();
}
if (keyLeft)
{
//向左移動
cube.transform.Translate(-Vector3.right * Time.deltaTime * 5);
check();
}
if (keyRight)
{
//向右移動
cube.transform.Translate(Vector3.right * Time.deltaTime * 5);
check();
}
}
//越界檢測
void check()
{
//得到當前主角在地圖中的坐標
float x = cube.transform.position.x;
float z = cube.transform.position.z;
//當控制主角超過地圖范圍時,重新計算主角坐標
if (x >= widthCheck)
{
x = widthCheck;
}
if (x <= -widthCheck)
{
x = -widthCheck;
}
if (z >= heightCheck)
{
z = heightCheck;
}
if (z <= -heightCheck)
{
z = -heightCheck;
}
cube.transform.position = new Vector3(x, cube.transform.position.y, z);
//根據比例計算小地圖“主角”的坐標
//GUI 坐標是左上角為起點和螢屏坐標以左下角為起點
mapcube_x = (map.width / mapWidth * x) + ((map.width / 2) - (map_cube.width / 2)) + (Screen.width - map.width);
mapcube_y = map.height - ((map.height / mapHeight * z) + (map.height / 2));
// mapcube_y =((map.height / mapHeight * z) + (map.height / 2));
}
}
極度不明白最后求小地圖主角坐標的兩行代碼,求大神做詳細解釋!!!
uj5u.com熱心網友回復:
這問題沒有大神愿意回答么轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/61387.html
標籤:Unity3D
上一篇:關于提取某3d游戲的地圖
