Unity 控制游戏对象,显示小地图

Hi,推荐文件给你 "小地图相关素材.zip" http://vdisk.weibo.com/s/LdtOl

using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour {
	
	
	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;
		mapHeight = mapHeight / 2;
		Check();
	}
	void OnGUI()
	{
		keyUp = GUILayout.RepeatButton("Up");
		keyDown = GUILayout.RepeatButton("Down");
		keyLeft = GUILayout.RepeatButton("Left");
		keyRight = GUILayout.RepeatButton("Right");
		//绘制小地图背景
		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(new Vector3(0,0,5 * Time.deltaTime));
			Check();
		}
		
		if(keyDown)
		{
			//向后移动
			cube.transform.Translate(-Vector3.forward * Time.deltaTime * 5);
			Check();
		}
		
		if(keyLeft)
		{
			//向左移动
			cube.transform.Translate(Vector3.left * 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;
		}
		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 () {
	
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜甲同学

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值