——看一下当时的笔记表示:我也是做过VR游戏的
一、视角判读
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
//public GameObject obj;
//public GameObject obj1;
private bool b=true;
//private Vector3 normal=new Vector3(10,10,10);
public bool isRendering = false;
private float lastTime = 0;
private float CurTime = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// Debug.Log ("角度"+Vector3.Dot (transform.forward.normalized, obj.transform.position.normalized));
// Debug.Log ("距离"+Vector3.Distance (obj.transform.position, transform.position));
isRendering = CurTime != lastTime ? true : false;
lastTime = CurTime;
if (isRendering&&b) {
run ();
}
}
void OnWillRenderObject(){
CurTime = Time.time;
}
//创建cube
/*
void run(){
GameObject cube=GameObject.CreatePrimitive (PrimitiveType.Cube);
//cube.GetComponent<Transform> ().position = (GameObject.Find("Camera")).GetComponent<Transform> ().position+normal*0.1f;
cube.GetComponent<Transform> ().position = -((GameObject.Find("Camera")).GetComponent<Transform> ().position)+2f*((GameObject.Find("lazhu")).GetComponent<Transform> ().position);
Debug.Log ("asssssssssssssssssssssssssss");
b = false;
}
*/
//改变鬼的位置
void run(){
GameObject.Find("aa").GetComponent<Transform>().position = -((GameObject.Find("Camera")).GetComponent<Transform> ().position)+2f*((GameObject.Find("lazhu")).GetComponent<Transform> ().position);
}
}
二、时间控制事件
using UnityEngine;
using System.Collections;
//using System.Diagnostics;
public class time : MonoBehaviour {
public static float t=0;
public GameObject obj;
private static bool b=true;
private float m = 5 ;
void Start () {
}
void Update(){
t+= Time.deltaTime;
//Debug.Log (t);
if (t > m) {
create ();
}
}
void create(){
if(b)
obj.SetActive (true);
b = false;
}
}
三、距离控制事件
using UnityEngine;
using System.Collections;
public class clock : MonoBehaviour {
//目标物体距离大于..多少执行代码
public GameObject player=null;
private static bool bo=true ;
private static float t = 0;
Vector3 f2 ;
AudioSource au =null ;
public AudioClip source;
//距离小于20,时间大于100开始响钟声
// Use this for initialization
void Start () {
au= gameObject.GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
Vector3 f1 = transform.position;
f2= player.transform.position;
float dis=Vector3.Distance (f1, f2);
t += Time.deltaTime;
Debug.Log("距离"+dis);
if (t > 100&&bo&&dis<20) {
Debug.Log (t);
au.clip = source;
au.Play ();
bo = false;
}
}
}
四、手柄控制脚步并发出声音
using UnityEngine;
using System.Collections;
using VRTK;
public class jiaobusheng : MonoBehaviour {
public GameObject outGround;
public GameObject louti;
public GameObject inground;
// Use this for initialization
void Start () {
outGround.GetComponent<AudioSource> ().enabled=false;
}
void Update()
{
VRTK_ControllerEvents events = GetComponent<VRTK_ControllerEvents> ();
events.TouchpadPressed += new ControllerInteractionEventHandler (OnWalk);
VRTK_ControllerEvents events1 = GetComponent<VRTK_ControllerEvents> ();
events1.TouchpadReleased += new ControllerInteractionEventHandler (DontWalk);
}
private void OnWalk(object sender,ControllerInteractionEventArgs events){
outGround.GetComponent<AudioSource> ().enabled=true;
}
private void DontWalk(object sender,ControllerInteractionEventArgs events)
{
outGround.GetComponent<AudioSource> ().enabled=false;
}
}
五、控制灯光闪烁
using UnityEngine;
using System.Collections;
public class lightCDon : MonoBehaviour {
private static float time=0;
public GameObject lightc=null;
// Use this for initialization
void Start () {
}
void Update (){
time += Time.deltaTime;
int cout = (int)(time % 3);
switch (cout) {
case 0:
lightc.SetActive (true);
break;
case 1:
lightc.SetActive (false);
break;
case 2:
lightc.SetActive (true);
break;
}
}
// Update is called once per frame
}
六、声音控制
using UnityEngine;
using System.Collections;
public class bofang : MonoBehaviour {
public GameObject obj;
private Vector3 f1;
private Vector3 f2;
private static float t=0;
public AudioClip[] source;
private AudioSource As;
// Use this for initialization
void Start () {
As = this.GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
f1 = transform.position;
t+= Time.deltaTime;
if (t > 1) {
f2 = transform.position;
t = 0;
}
float dis = Vector3.Distance (f1, f2);
//float f=Vector3.Distance (obj.transform.position, transform.position);
if (dis>0.8) {
play ();
}
}
void play(){
As.clip = source [1];
As.Play () ;
f2 = f1;
}
}