標籤:blog http io ar os sp for on div
這次的作業是二選一,我綜合了自己的興趣以及情況,我選擇了做一個3D遊戲來完成。在查閱了很多資料之後,我選擇了Unity3d來學習,因為它內建強大的編輯器,易於我們這些新人上手,在學習了兩個星期後,我決定做一個之前很火的flappy bird來練手,下面是具體完成的情況。
設計思路:利用U3D內建的物理碰撞引擎去檢測碰撞事件,在每幀重畫的時候去選擇小鳥形態的切換,給小鳥的一個水平的速度,然後在情境上設定一個觸發器,負責情境的延生,這樣遊戲就可以源源不斷的進行下去。
主要代碼如下:
using UnityEngine;using System.Collections;public class Bird : MonoBehaviour {public float timer=0;public int frameNumber= 10;// frame number one secondspublic int frameCount=0;// frame counterpublic bool animation=false;// whether play the fly animationpublic bool canJump=false;void Start(){//this.rigidbody.velocity = new Vector3(2,0,0);}// Update is called once per framevoid Update () {//hard code here for test//Vector3 vel = this.rigidbody.velocity;//this.rigidbody.velocity = new Vector3(5,vel.y,vel.z);// animationif(GameManager._intance.GameState==GameManager.GAMESTATE_PLAYING){timer+=Time.deltaTime;if(timer>=1.0f/frameNumber){frameCount++;timer-=1.0f/frameNumber;int frameIndex = frameCount%3;// update material ‘s offset x// how to set the property of(x offset) MainTex : Main Texturethis.renderer.material.SetTextureOffset("_MainTex",new Vector2(0.333333f*frameIndex,0));//this.renderer.material.SetTextureScale("_MainText",new Vector2(1,1));}}if(GameManager._intance.GameState==GameManager.GAMESTATE_PLAYING){// control jumpif(Input.GetMouseButton(0) ){// left mouse button down audio.Play();Vector3 vel2 = this.rigidbody.velocity;this.rigidbody.velocity = new Vector3(vel2.x,5,vel2.z);}}}public void getLife(){rigidbody.useGravity=true;this.rigidbody.velocity = new Vector3(2,0,0);}}
using UnityEngine;using System.Collections;public class GameManager : MonoBehaviour {// define the state of the gamepublic static int GAMESTATE_MENU =0;//遊戲菜單狀態public static int GAMESTATE_PLAYING=1;//遊戲中狀態 ... public static int GAMESTATE_END=2;//遊戲結束狀態public Transform firstBg;public int score = 0;public int GameState = GAMESTATE_MENU;public static GameManager _intance;private GameObject bird;void Awake(){_intance = this;bird = GameObject.FindGameObjectWithTag("Player");}void Update(){if(GameState==GameManager.GAMESTATE_MENU){if(Input.GetMouseButtonDown(0)){// transform stateGameState = GAMESTATE_PLAYING;// set bird is playing // 1.set gravity 2.add velocity of xbird.SendMessage("getLife");}} if (GameState == GameManager.GAMESTATE_END) { GameMenu._instance.gameObject.SetActive(true); GameMenu._instance.UpdateScore(score); }}}
using UnityEngine;using System.Collections;public class MoveTrigger : MonoBehaviour {public Transform currentBg;public Pipe pipe1;public Pipe pipe2;public void OnTriggerEnter(Collider other){print("OnTriggerEnter");if(other.tag=="Player"){// move the bg to the front of first transform//1. get the first transfromTransform firstBg= GameManager._intance.firstBg;// 2. movecurrentBg.position = new Vector3( firstBg.position.x+10,currentBg.position.y,currentBg.position.z);GameManager._intance.firstBg = currentBg;// new position for pipepipe1.RandomGeneratePosition();pipe2.RandomGeneratePosition();}}}
心得體會:
通過這次U3D的學習,我有機會去瞭解3d程式的基本概念以及開發過程,在學習的過程中,我遇到了很多問題,通過查閱資料與諮詢他人將這些問題都一一解決。從此之後,我覺得自己對編程也抱有更大的熱情,受益匪淺。
軟體工程第二次作業