作業一 : 期末專題
一、今日目標
今天進度 :
1. 手把可拿起物品
2. 飛標可弄破氣球
3. 套圈圈碰撞設定
二、成果
1. 手把程式(拿和丟的動作)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))]
public class ThrowObject : MonoBehaviour {
SteamVR_TrackedObject trackedObj;
SteamVR_Controller.Device device;
public float speedUp=3.0f;
public GameObject Ball;
void Awake () {
trackedObj = GetComponent<SteamVR_TrackedObject> ();
}
void FixedUpdate () {
device = SteamVR_Controller.Input ((int)trackedObj.index);
if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Grip)) {
GameObject ball=GameObject.Instantiate (Ball);
ball.transform.position = new Vector3 (0f, 1f, -0.5f);
}
}
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) {
rigidBody.velocity=device.velocity*speedUp;
rigidBody.angularVelocity=device.angularVelocity;
}
}
2. 射氣球
加入程式碼並設碰撞及tag,即可成功。![]() |
| 紅色氣球,藍色當飛鏢 |
程式 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour {
public GameObject ball1;
void OnCollisionEnter( Collision Ball){
if (Ball.gameObject.tag == "ball1") {
ball1.gameObject.SetActive (false);
}
}
}
3. 套圈圈
設好物理碰撞及重量即可成功。
![]() |
| 紅色當套圈圈道具 |


沒有留言:
張貼留言