1.將事先建好的七巧板模型匯入Unity,並加入Mesh Coilder以利後面做碰撞偵測

2.加入程式碼拿起七巧板

程式碼如下:
void OnTriggerStay(Collider col) {
if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger) && col.tag!="desk"){
//Debug.Log ("抓起");
// 將 physics(物理引擎) 關掉
col.attachedRigidbody.isKinematic = true;
// 設定controller為parent
col.gameObject.transform.SetParent(gameObject.transform);
}
if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) {
col.gameObject.transform.SetParent (null);
col.attachedRigidbody.isKinematic = false;
//GameObject.Destroy (col.gameObject, 3f);
//tossObject (col.attachedRigidbody);
}
}
3.過關條件判斷
單個七巧板放置在木板上時,設定布林值為真。當所有的七巧板都放在木板上,每塊七巧板的布林值皆為真,即達成過關條件
布林值條件程式碼:
void OnCollisionStay(Collision other) {
if(other.gameObject.tag=="desk")
blue=true;
}
void OnCollisionExit(Collision other) {
if(other.gameObject.tag=="desk")
blue=false;
}
過關條件程式碼:
void Update () {
device = SteamVR_Controller.Input ((int)trackedObj.index);
if(col_blue.blue&&col_gray.gray&&col_green.green&&col_orange.orange&&col_yellow.yellow&&col_purple.purple&&col_red.red&&col_black.black)
{
puzzover=true;
SceneManager.LoadScene ("start_menu");
}
}
4.加入移動角色的程式碼,好移動拿取七巧板
(以原先旋轉物體的程式碼做修改)
程式碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tranlate : MonoBehaviour
{
// Start is called before the first frame update
SteamVR_TrackedObject trackedObj;
SteamVR_Controller.Device device,deviceLeft,deviceRight;
public GameObject obj;
float a=0.0f,b=0.0f,c=0.0f;
Vector2 rotat=new Vector2(0.0f,0.0f);
void Start()
{
trackedObj=GetComponent<SteamVR_TrackedObject>();
a=obj.transform.position.x;
b=obj.transform.position.y;
c=obj.transform.position.z;
}
// Update is called once per frame
void Update()
{
device = SteamVR_Controller.Input ((int)trackedObj.index);
if (device.GetPress (SteamVR_Controller.ButtonMask.Touchpad)){
rotat = device.GetAxis ();
float angle=Vector2.Angle (new Vector2 (1, 0), rotat);
if (angle > 45 && angle < 135) {
if (rotat.y > 0)
c+=0.02f;
else
c-=0.02f;
}
else {
if (rotat.x>0)
a+=0.02f;
else
a-=0.02f;
}
}
obj.transform.position = (new Vector3 (a, b, c));
沒有留言:
張貼留言