unity + win8.1 apps 小遊戲demo

來源:互聯網
上載者:User

標籤: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] 聯絡我,非常感謝。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.