2017年4月4日 星期二

Week06 實做加影片

1. 案住Trigger後可產生多個方塊

程式碼 : 

成果 :


2. 抓取物體

程式碼 : 


成果 : 


3. 光劍

程式碼 : 


成果 :



4. HitBottleVR 丟瓶子

程式碼 : 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(SteamVR_TrackedObject))]
public class ThrowObjectResetBottle : MonoBehaviour {

SteamVR_TrackedObject trackedObj;
SteamVR_Controller.Device device;
public float speedUp=3.0f;

// 將來代入兩個prefab
public GameObject Ball,Bottle;

// 記錄BottleFall 程式的參考變數
BottleFall bottleFallScript;

// 記錄目前產生的球的參考變數
GameObject ball;

        void Awake () 
       {
                 trackedObj = GetComponent<SteamVR_TrackedObject> ();
                 // 目前沒有產生的球
                 ball = null;
                //先找到BottleStand物件, 再找到BottleFall.cs程式,並記錄下來
                bottleFallScript = GameObject.Find ("BottleStand").GetComponent<BottleFall>();

       }

       void FixedUpdate () 
      {
               device = SteamVR_Controller.Input ((int)trackedObj.index);
               if (device.GetTouchUp (SteamVR_Controller.ButtonMask.Grip))
              {
                         if (ball == null) 
                        {
                                   // 目前沒有球,就產生一顆
                                   ball = GameObject.Instantiate (Ball);

                                   // 固定住, 不受重力與物理引擎的作用
                                   ball.GetComponent<Rigidbody> ().isKinematic = true;

                                  // 移動至想要的位置
                                   ball.transform.position = new Vector3 (0.5f, 1f, 0f);
                        }

                       if (! bottleFallScript.bottleOnTable) 
                      {

                                // 如果桌上沒瓶子有,就產生一個
                               GameObject bottle = GameObject.Instantiate (Bottle);

                               // 找到桌子的位置
                               Vector3 tablePos = GameObject.Find ("Table").transform.position;

                               // 將瓶子放在上方 0.8個單位的地
                               bottle.transform.position = new Vector3 (0f, 0.8f, 0f) + tablePos;

                              // 設定 bottleOnTable 設定為真
                              bottleFallScript.bottleOnTable = true;
                     }
           }

     }

    void OnTriggerStay(Collider col) 
     {
             Debug.Log ("碰到");
             if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
            { 
                          Debug.Log ("抓起");

                         // 將 physics(物理引擎) 關掉
                         col.attachedRigidbody.isKinematic = true;

                        // 設定controller為parent
                        col.gameObject.transform.SetParent(gameObject.transform);
             }

            if (device.GetTouchUp (SteamVR_Controller.ButtonMask.Trigger)) 
           {
                       Debug.Log ("放開");

                     // 將球的parent設定為空的,解除父子關係
                     col.gameObject.transform.SetParent (null);

                    // 讓球受物理引擎(重力)影響
                    col.attachedRigidbody.isKinematic = false;

                   // 將球設定 3秒鐘之後自動銷毀
                   GameObject.Destroy (col.gameObject, 3f);

                  // 設定目前沒有可丟的球
                   ball = null;

                  // 找到HUD物件,在Ranking.cs程式中,記錄丟球個數增加 1
                  GameObject.Find ("HUD").GetComponent<Ranking> ().tossInc ();

                 // 將球丟出去
                 tossObject (col.attachedRigidbody);
         }

     }

       void tossObject(Rigidbody rigidBody)   
      {
                  // 球繼承controller的速度與轉速
                  rigidBody.velocity=device.velocity*speedUp;
                  rigidBody.angularVelocity=device.angularVelocity;
      }


}

成果 : 

沒有留言:

張貼留言