Unity學習筆記六

接着筆記五:
此次主要內容是:
  • 點擊怪物的時候發送子彈,子彈打中怪物的時候怪物血量纔會減小
  • 子彈數量視圖
  • 主角剩餘血量視圖
  • 子彈夾,醫藥箱功能

1.先建立一個子彈:GameObject -> Create Other -> sphere ->更名:bullet
而後將球體縮小到必定的程度,給球體添加一個貼圖,並給子彈添加剛體組件:


2.新建一個子彈預設,把上面作好的子彈拖到預設。

3.而後建立一個空的場景對象:GameObject -> Create Empty, 更名firePoint,這個對象的主要做用是保存
子彈的發射的起點,而且修改腳本:RoleMoveController,讓這個空對象始終保持在主角(攝像機)
的前面:
      //發射位置
    private GameObject firePoint;

     // Use this for initialization
     void Start () {
        firePoint = GameObject.Find("firePoint");
     }

     void Update () {
          if (Input.GetKey (KeyCode.A)) {
               this.transform.Translate(Vector3.left*Time.deltaTime*Common.MOVE_SPEED);
          }
          else if (Input.GetKey (KeyCode.D)) {
               this.transform.Translate(Vector3.right*Time.deltaTime*Common.MOVE_SPEED);
          }
          else if (Input.GetKey (KeyCode.W)) {
               this.transform.Translate(Vector3.forward*Time.deltaTime*Common.MOVE_SPEED);
          }
          else if (Input.GetKey (KeyCode.S)) {
               this.transform.Translate(Vector3.back*Time.deltaTime*Common.MOVE_SPEED);
          }
            //讓發射點始終保持在主角的前面
         firePoint.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 3);
     }
     

4.修改RoleBulletController,相應鼠標左鍵發射子彈,並在左下角實時顯示剩餘子彈數:

     //受重力影響的子彈
     public Rigidbody bullet;

    //發射位置
    private GameObject firePoint;
//受重力影響得子彈
     public Rigidbody bullet;

    //發射位置
    private GameObject firePoint;

     // Use this for initialization
     void Start () {
          role = GameObject.Find ("role");
        firePoint = GameObject.Find("firePoint");
     }
    
     // Update is called once per frame
     void Update () {
        //點擊鼠標左鍵
        if (Input.GetMouseButtonDown(Common.MOUSE_BTN_LEFT) && bullets > 0)
        {
               //剩餘子彈個數
               bullets--;
            //經過射線得到目標點
               //Returns a ray going from camera through a screen point.
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
               //Returns a point at distance units along the ray.
            Vector3 target = ray.GetPoint(20);
               //修改發射起點的朝向
            firePoint.transform.LookAt(target);
            //實例化子彈
            Rigidbody clone = (Rigidbody)Instantiate(bullet, firePoint.transform.position, firePoint.transform.rotation);
            //初始化子彈的方向速度
            clone.velocity = target - firePoint.transform.position;
        }
     }

     void OnGUI()
     {
          GUI.Label (new Rect (10, Screen.height - 30, 100, 50), "子彈個數 x"+bullets);
     }

5.運行遊戲,會發現,當子彈打中怪物的時候,怪物會後退,咱們不要這個選項,因此在選中怪物的
預設,選中其剛體組件,勾選 isKinematic屬性,那麼這個物體將只有碰撞屬性不具有物理學屬性。

6.接下來修改EnemyController,讓怪物在與子彈發生碰撞以後才扣血:
修改原來方法OnMouseDown爲OnCollisionEnter:
      //怪物響應碰撞事件
     void OnCollisionEnter(Collision collision)
     {
          if (blood > 0) {
               isAttacked = true;
               blood = blood - 5;
               if (0 >= blood)
               {
                    dead();
               }
          }
          //子彈碰到怪物後消失
          Destroy (collision.gameObject);
     }

7.修改RoleBloodController,用來實時顯示主角的剩餘血量:
      //初始血量
     private int Blood = 100;

     public void reduceBlood(int reduceNum)
     {
          Blood = Blood - reduceNum;
           //失敗的時候跳回菜單頁面從新進入遊戲
          if (0 >= Blood) 
          {
               Application.LoadLevel("MainScene");
               SceneController.currentGameState = GameState.GAME_MENU;
          }
     }

     void OnGUI()
     {
          GUI.Label (new Rect (10, Screen.height - 70, 100, 50), "剩餘血量 x"+Blood);
     }

8.接下來就是實現醫藥箱跟子彈夾功能,先把醫藥箱跟子彈夾作成預設,而後給它們分別添加
標籤:Medicine和BulletBox, 而後在RoleBloodController中修改:
      //場景中的醫藥包
     private GameObject[] medicines;

     // Use this for initialization
     void Start () {
          medicines = GameObject.FindGameObjectsWithTag ("Medicine");
     }
     
      // Update is called once per frame
     void Update () {
          for (int i= 0; i < medicines.Length; i++) 
          {
               if(null == medicines[i]) continue;
                 //當醫藥箱與主角距離小於2的時候被主角拾取
               if(Vector3.Distance(transform.position, medicines[i].transform.position) <= 2)
               {
                    Blood = Blood + 50;
                    Destroy(medicines[i]);
                    medicines[i] = null;
               }
          }

       }

子彈夾同理。

9.運行程序~!oh ye...終於作完. this

9.運行程序~!oh ye...終於作完。

10.導出程序:


相關文章
相關標籤/搜索