2017年3月28日 星期二

Week 5

本周由蕭立人老師教課,結合Vive使用
教材 :
https://drive.google.com/drive/folders/0B3U4TWW5it5nQkRUaWV1dTdXenM?usp=sharing

1. Seasaw

using UnityEngine;
using System.Collections;

public class ReverionScript : MonoBehaviour {
GameObject personA,personB;
Rigidbody rigidA,rigidB;
float heavyWeight=20.0f; // 要往下掉的角色,較重的質量
float lightWeight=1f; // 要往上昇的角色,較輕的質量
// Use this for initialization
void Start () {
// 以 Tag 找到 Person A 與 Person B 兩個物件並記錄下來
personA = GameObject.FindGameObjectWithTag ("Person A");
personB = GameObject.FindGameObjectWithTag ("Person B");
rigidA = personA.GetComponent<Rigidbody> ();
rigidB = personB.GetComponent<Rigidbody> ();
}

void OnTriggerEnter(Collider other)
{
// 碰撞到時觸發的擯制程式
if (other.tag=="Person A"){
// 目前落地的是 Person A,質量改小
rigidA.mass=lightWeight;
// 對方是 Person B,質量改大
rigidB.mass=heavyWeight;
}
else if (other.tag=="Person B"){
//  目前落地的是 Person B,質量改小
rigidB.mass=lightWeight;
// 對方是 Person A,質量改大
rigidA.mass=heavyWeight;
}
}
}


2. Campass

(指南針移動)
using UnityEngine;
using System.Collections;

public class CompassMoving : MonoBehaviour {

       void Update () 
      {
              // 鍵盤的水平移動用來控制碟盤旋轉
              float theda=Input.GetAxis("Horizontal");
              transform.Rotate(0.0f,theda,0.0f);
              Debug.Log (transform.forward);
              // 鍵盤的重直移動用來控制碟盤前進與後退
              Vector3 forewd=transform.forward;
              float dz=Input.GetAxis ("Vertical");
              Debug.Log (transform.forward*dz);
              transform.position = transform.position+transform.forward * dz;
       }

}

(攝影機跟隨指南針移動)
using UnityEngine;
using System.Collections;

public class FollowingCamera : MonoBehaviour {
// 須將compass套進來,才知道目標

public GameObject target;

      // Use this for initialization

        void Start () 
       {
       }

       // Update is called once per frame
       void Update ()
      {
               // 跟隨指南針的 Camera
               Vector3 pos = target.transform.position-target.transform.forward*2.0f;
               pos.y+=3.0f;
               transform.position=pos;
               transform.LookAt (target.transform.position);
       }

}

(指針方位不變)
using UnityEngine;
using System.Collections;

public class PinRotation : MonoBehaviour {

        void Update () 
       {
                // 取得parent的旋轉角度
                float parentAngle=transform.parent.transform.rotation.eulerAngles.y;

               // 與parent反方向旋轉
               transform.localRotation = Quaternion.Euler (0, -parentAngle, 0);
       }
} 

3. Toss

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

// 宣告程式需要的 SteamVR_TrackedObject Component
[RequireComponent(typeof(SteamVR_TrackedObject))]

public class ThrowObject3 : MonoBehaviour {

SteamVR_TrackedObject trackedObj;
SteamVR_Controller.Device device;

          void Awake ()
          {
                   trackedObj = GetComponent<SteamVR_TrackedObject> ();
          }

          void FixedUpdate () 
          {
                   device = SteamVR_Controller.Input ((int)trackedObj.index);
          }

           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 ("放開");
                              col.gameObject.transform.SetParent (null);
                              col.attachedRigidbody.isKinematic = false;
                              tossObject (col.attachedRigidbody);
                 }
         }

          void tossObject(Rigidbody rigidBody) 
        {
                    Transform origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent ;
                   
                    if (origin != null)
                   {
                          // 轉換速度與轉速到世界空間
                          rigidBody.velocity=origin.TransformVector(device.velocity);
                          rigidBody.angularVelocity=origin.TransformVector(device.angularVelocity);
                    }
                    else 
                    {
                           rigidBody.velocity = device.velocity;
                           rigidBody.angularVelocity = device.angularVelocity;
                    }
        }
}

沒有留言:

張貼留言