課堂實作:
2017年4月7日 星期五
2017年4月5日 星期三
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;
}
}
成果 :
程式碼 :
成果 :
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;
}
}
成果 :
2017年3月30日 星期四
week06
到s205實作
用手把偵測產生設定好的prefabs物件
然後就可以做出類似畫畫的效果



.
程式碼如下
using UnityEngine;
using System.Collections;
using Valve.VR;
public class Create : MonoBehaviour {
public SteamVR_TrackedObject rightController;
public GameObject redCube;
public GameObject blueCube;
public GameObject box;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var device = SteamVR_Controller.Input ((int)rightController.index);
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) {
Instantiate (redCube, rightController.transform.position, rightController.transform.rotation, box.transform);
}
if (device.GetPress (SteamVR_Controller.ButtonMask.Trigger)) {
Instantiate (blueCube, rightController.transform.position, rightController.transform.rotation, box.transform);
}
if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) {
foreach (Transform child in box.transform) {
GameObject.Destroy (child.gameObject);
}
}
}
}
第二個實作
是要把場景中的物件抓取起來
按下版機就可以當手一樣 把物件抓取起來

程式碼如下:
using UnityEngine;
using System.Collections;
public class Touch : MonoBehaviour {
public SteamVR_TrackedObject leftController;
bool isTouch=false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var device = SteamVR_Controller.Input ((int)leftController.index);
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) {
isTouch = true;
}
if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) {
isTouch = false;
}
}
void OnTriggerStay(Collider other){
if (other.tag == "catch" && isTouch) {
other.transform.position = this.transform.position;
}
}
}
第三個實作(回家作業)
將模型套入
讓手把在實境內是以光劍模組顯示


成功後就可以像絕地武士一樣揮舞光劍了!!!!
程式碼如下:
using UnityEngine;
using System.Collections;
public class LightSaberContol : MonoBehaviour {
public SteamVR_TrackedObject Controller;
public GameObject centerBeam;
lightsaber saberOn,saberCenterBeam;
void Start () {
saberOn = this.GetComponent<lightsaber> ();
saberCenterBeam = centerBeam.GetComponent<lightsaber> ();
}
// Update is called once per frame
void Update () {
if (Controller == null)
return;
var device = SteamVR_Controller.Input ((int)Controller.index);
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Grip)) {
saberOn.saberon = !saberOn.saberon;
saberCenterBeam.saberon = !saberCenterBeam.saberon;
print ("Grip");
}
}
}
用手把偵測產生設定好的prefabs物件
然後就可以做出類似畫畫的效果



.
程式碼如下
using UnityEngine;
using System.Collections;
using Valve.VR;
public class Create : MonoBehaviour {
public SteamVR_TrackedObject rightController;
public GameObject redCube;
public GameObject blueCube;
public GameObject box;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var device = SteamVR_Controller.Input ((int)rightController.index);
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) {
Instantiate (redCube, rightController.transform.position, rightController.transform.rotation, box.transform);
}
if (device.GetPress (SteamVR_Controller.ButtonMask.Trigger)) {
Instantiate (blueCube, rightController.transform.position, rightController.transform.rotation, box.transform);
}
if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) {
foreach (Transform child in box.transform) {
GameObject.Destroy (child.gameObject);
}
}
}
}
第二個實作
是要把場景中的物件抓取起來
按下版機就可以當手一樣 把物件抓取起來

程式碼如下:
using UnityEngine;
using System.Collections;
public class Touch : MonoBehaviour {
public SteamVR_TrackedObject leftController;
bool isTouch=false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var device = SteamVR_Controller.Input ((int)leftController.index);
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) {
isTouch = true;
}
if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) {
isTouch = false;
}
}
void OnTriggerStay(Collider other){
if (other.tag == "catch" && isTouch) {
other.transform.position = this.transform.position;
}
}
}
第三個實作(回家作業)
將模型套入
讓手把在實境內是以光劍模組顯示


成功後就可以像絕地武士一樣揮舞光劍了!!!!
程式碼如下:
using UnityEngine;
using System.Collections;
public class LightSaberContol : MonoBehaviour {
public SteamVR_TrackedObject Controller;
public GameObject centerBeam;
lightsaber saberOn,saberCenterBeam;
void Start () {
saberOn = this.GetComponent<lightsaber> ();
saberCenterBeam = centerBeam.GetComponent<lightsaber> ();
}
// Update is called once per frame
void Update () {
if (Controller == null)
return;
var device = SteamVR_Controller.Input ((int)Controller.index);
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Grip)) {
saberOn.saberon = !saberOn.saberon;
saberCenterBeam.saberon = !saberCenterBeam.saberon;
print ("Grip");
}
}
}
2017年3月29日 星期三
Week 06 D:
week06_拿起物件
今天上課的主題是透過grip、trigger來呈現拿取物件的效果
在實驗室裡我們先嘗試了應用trigger將物件拿起、移動和放下,放下的部分要看有沒有將 Is Kinematic的選項打勾,有打勾表示我們的cube物件不會有重力的向下掉,可以在我們那起的時候停在空中。
接著我們想嘗試的是如何將物件丟出去,因為今天所是造的物件只要一丟出去就會有重力的往下掉,沒辦法拋到遠處,所以接下來我們想試著嘗試丟擲這部分的動作。
Week06_Unity VR_isTragger應用_呂登祐
在場景新增一個cube物件,然後再Add Componet增加一個Box Collider做碰撞偵測,在撰寫catchcube.cs碰撞的程式碼(如下),以及Add Componet一個Rigidbody,因為我們要用程式碼去控制手把去拿物體,所以我們要把"Is Kinematic"險項勾選起來,這樣程式碼就可以控制拿物體的時候,手放開Cube不會自己掉下去,而是手放開的時候會停格在半空中。
using UnityEngine;
using System.Collections;
using Valve.VR;
public class catchcube : MonoBehaviour {
public SteamVR_TrackedObject leftController;
bool isTouch = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var device = SteamVR_Controller.Input ((int)leftController.index);
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) {
isTouch = true;
}
if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) {
isTouch = false;
}
}
void OnTriggerStay(Collider other){
if (other.tag == "catch" && isTouch)
other.transform.position = this.transform.position;
}
}
week06 03160595
1.s207實作
網址:
https://docs.google.com/presentation/d/1qUlepP42SzdRVYRDsVr6EoA3VOPyT_3pIheQKVva3Nw/edit#slide=id.g202451d095_0_101
第一個project
網址:
https://docs.google.com/presentation/d/1qUlepP42SzdRVYRDsVr6EoA3VOPyT_3pIheQKVva3Nw/edit#slide=id.g202451d095_0_101
第一個project
手把控制 按下時會有方塊出來
壓著就可以開始畫
程式
第二個project
抓取物體
程式
第三個project
light saber-光劍
2.看影片
進度:第六個project
訂閱:
文章 (Atom)















