小地图

using UnityEngine;
using System.Collections;

public class CreateMap : MonoBehaviour {

    protected GameObject plane;
    protected GameObject cube;

    protected float mapWidth;
    protected float mapHeight;
    protected float widthCheck;
    protected float heightCheck;

    protected float mapCube_x=0;
    protected float mapCube_y=0;

    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 scale_x=plane.transform.localScale.x; //默认缩放比例
        float size_z=plane.GetComponent<MeshFilter>().mesh.bounds.size.z;
        float scale_z=plane.transform.localScale.z;

        //原始宽度*缩放比例=实际宽度
        mapWidth=size_x*scale_x;
        mapHeight=size_z*scale_z;

        widthCheck =mapWidth/2;
        heightCheck =mapHeight/2;

        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/mapWidth*x)+((map.width/2)-(map_cube.width/2))+(Screen.width-map.width);
        mapCube_y=map.height-((map.height/mapHeight *z )+(map.height/2));

    }
    // Update is called once per frame
    void Update () {
        if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
        {
            cube.transform.Translate(Vector3.forward*Time.deltaTime *5);
            check();
        }
        if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
        {
            cube.transform.Translate(Vector3.forward * (-Time.deltaTime) *5);
            check();
        }
    }

    void OnGUI()
    {
        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);
    }
}

 

posted @ 2014-08-21 16:58  hometown  阅读(243)  评论(0编辑  收藏  举报