小地形
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 () {
}
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 () {
}
相关文章推荐
- 在 Linux 中如何移动文件
- PowerShell移动目录中指定文件的方法(非全部文件)
- 鼠标触发移动的分层菜单 层菜单moveMenu
- javascript实现图片跟随鼠标移动效果的方法
- JavaScript Select和Option列表元素上下左右移动
- jquery移动点击的项目到列表最顶端的方法
- C#中无边框窗体移动的简单实例
- c#文件的复制,移动,创建(实例代码)
- 多选列表框动态添加,移动,删除,全选等操作的简单实例
- jQuery实现div浮动层跟随页面滚动效果
- javascript:文字不间断向左移动的实例代码
- JS控制对象移动效果
- 移动指定文件夹内的全部文件
- 在Google 地图上实现做的标记相连接
- PHP计算百度地图两个GPS坐标之间距离的方法
- PHP递归复制、移动目录的自定义函数分享
- sogou地图API用法实例教程
- 移动Web中图片自适应的两种JavaScript解决方法
- 2014值得推荐的10个移动 Web 应用程序开发框架
- unity实现多点触控代码