2019年6月4日 星期二

Week16_黃登煜

1.解決上次搞不懂的很多關於gameobject等等的問題
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class ViveInput : MonoBehaviour
{
public GameObject CameraRig;
public GameObject objectTouched=null;
Quaternion oldRotation;
public SteamVR_Action_Boolean triggerDown;
public SteamVR_Action_Vibration hapticAction;
public SteamVR_Action_Single squeezeAction;
public SteamVR_Action_Vector2 touchpadTouch;
public GameObject controller;
// Start is called before the first frame update
GameObject theThingYouTouch;
void Start()
{
}
bool bPressed=false;
Vector2 oldPos;
Vector3 oldPostion;
// Update is called once per frame
void Update()
{
Vector2 padPos = touchpadTouch.GetAxis(SteamVR_Input_Sources.Any);
if(padPos != Vector2.zero){
if(bPressed==false){
oldPos = padPos;
bPressed=true;
}
}else
{
bPressed=false;
oldPos=Vector2.zero;
}
Vector2 goSpeed2 = padPos-oldPos;
Vector3 goSpeed3 = new Vector3(goSpeed2.x, 0, goSpeed2.y);

Quaternion headDirection = CameraRig.transform.rotation;///SteamVR CameraRig 's rotation of HMD

Vector3 realSpeed = headDirection * goSpeed3;
CameraRig.transform.position += realSpeed*0.005f;

if(triggerDown.GetState(SteamVR_Input_Sources.RightHand))
{
hapticAction.Execute(0, 1, 150, 75, SteamVR_Input_Sources.RightHand);
print ("triggerDown");

}
if(triggerDown.GetState(SteamVR_Input_Sources.LeftHand))
{
hapticAction.Execute(0, 1, 150, 75, SteamVR_Input_Sources.LeftHand);
print ("triggerDown");

}
float triggerValue = squeezeAction.GetAxis(SteamVR_Input_Sources.Any);
//Vector2 padPos = touchpadTouch.GetAxis(SteamVR_Input_Sources.Any);
if(triggerDown.GetState(SteamVR_Input_Sources.Any) && objectTouched!=null){
if(bPressed==false){
oldPostion = controller.transform.position;
oldRotation = controller.transform.rotation;
bPressed=true;
}
Vector3 diff=controller.transform.position - oldPostion;
objectTouched.transform.position += diff;
Quaternion diffAngle = Quaternion.FromToRotation(oldRotation.eulerAngles,
controller.transform.position);
objectTouched.transform.rotation *= diffAngle;   
oldPostion = controller.transform.position;
oldRotation = controller.transform.rotation;

}else{
bPressed=false;
}
if(triggerValue > 0.0f)
{
print(triggerValue);
}
}
void OnTriggerEnter(Collider other){
//objectTouched = other;
theThingYouTouch = other.gameObject;
}
void OnTriggerExit(Collider other){
if(objectTouched == other){
objectTouched = null;
}
}
}

2.一直製造紅色磚塊掉落
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class red : MonoBehaviour {
    public GameObject box;
    public Vector3 V3_Random;
    // Use this for initialization
    void Start () {
    int frameN=0;
    // Update is called once per frame
    void Update () {
        V3_Random=new Vector3(-6f,0.7f,
        Random.Range(-7f,-8f));
        Quaternion rotation = Quaternion.Euler(Random.Range(0,180),Random.Range(0,180),
        Random.Range(0,180));
        if(frameN%60==0){
            GameObject now =Instantiate(box,V3_Random, rotation);
        }
        frameN++;
    }
}

沒有留言:

張貼留言