码迷,mamicode.com
首页 > 其他好文 > 详细

小地图

时间:2014-08-21 18:39:24      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   ar   art   

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);
    }
}

 

小地图,布布扣,bubuko.com

小地图

标签:style   blog   color   os   io   for   ar   art   

原文地址:http://www.cnblogs.com/hometown/p/3927599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!