unity + win8.1 apps 小游戏demo

unity3d用的人挺多。

。本来想写个3d游戏试试。

。额。。貌似挺麻烦。。 。

。。

。。先用unity写个简单的2d游戏吧。。




(adsw回车  或者  触摸屏虚拟摇杆)


开发环境 unity4.5.1f3      vs2013 express       win8.1

实现方法例如以下

1.创建自己的坦克Sprite

2.创建敌方坦克Prefab

3.创建子弹Prefab

4.加入虚拟摇杆

5.碰撞检測




详细实现

1.控制自己坦克的代码

		if (Input.GetKey (KeyCode.W)) {
			Rotate (0);
				} 
		else  if (Input.GetKey (KeyCode.S)) {
			Rotate (180);
		} else if (Input.GetKey (KeyCode.A)) {
			Rotate (90);

		} else if (Input.GetKey (KeyCode.D)) {
			Rotate (270);
		}

		i++;
		if (i == 10) {i=0;
			if (Input.GetKey (KeyCode.Return)||rotateJoystick.tapCount > 0) {
								m.tag = "GOOD";
								Instantiate (m, transform.position, transform.rotation);
						}
				}


2.控制地方坦克的代码

		if (step == 0)
		{
			int i=Random.Range(0, 4);
			Rotate(angles[i]);
			step = Random.Range(0, 20) + 85;
			if(Random.Range(0,50)>40)
			{
				m.tag="BAD";
				Instantiate(m, transform.position, transform.rotation);
			}
		}
		else
		{
			step--;
		}
		transform.Translate (Vector3.up*Time.deltaTime*4);


3.控制子弹移动

transform.Translate (Vector3.up * Time.deltaTime * 10);


4.虚拟摇杆用的Untiy   里面  Standard Assets(Mobile).unityPackage自带的

5.碰撞检測代码

	void OnTriggerEnter2D(Collider2D mCollider)
	{
		if ((mCollider.gameObject.tag == "Enemy"&&gameObject.tag=="GOOD")||(mCollider.gameObject.tag == "Player"&&gameObject.tag=="BAD"))
		{
			Destroy(mCollider.gameObject);
			Destroy(this.gameObject);
			if(gameObject.tag=="BAD")
			{
				PlayerPrefs.SetString("key","OVER");
				Application.LoadLevel(1);
			}
		}
	}




代码下载:http://download.csdn.net/detail/xiaoxiao108/7674235


假设你发现有什么不合理的,须要改进的地方,请留言。或者能够通过 328452421@qq.com 联系我。很感谢。




原文地址:https://www.cnblogs.com/jhcelue/p/6735800.html