您的位置:首页 > 移动开发 > Unity3D

小地形

2015-07-22 11:14 429 查看
GameObject plane;//大地图
GameObject cube;//大地图主角
float mapWidth;//大地图宽度
float mapHeight;//大地图高度
float widthCheck;//边界检测数值
float heightCheck;
float mapcube_x=0;//小地图主角位置
float mapcube_y=0;
bool keyUp;//按钮开关
bool keyDown;
bool keyLeft;
bool keyRight;
public Texture map;//小地图背景贴图
public Texture map_cube;//小地图主角贴图
// Use this for initialization
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);
mapcube_x = (map.width * x / mapWidth ) + ((map.width / 2) - (map_cube.width / 2)) + (Screen.width - map.width);
mapcube_y = map.height - ((map.height * z / mapHeight) + (map.height / 2));
}
// Update is called once per frame
void Update () {

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity 移动 地图