首页 > 代码库 > 小地图
小地图
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); }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。