標籤:des blog http os io 2014 for re
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
如果你發現有什麼不合理的,需要改進的地方,請留言。或者可以通過 [email protected] 聯絡我,非常感謝。