一些unity3d的代码片段

本文详细介绍了使用Unity进行VR游戏开发的多个实用技巧,包括视角判断、时间控制、距离感应事件、手柄控制、灯光闪烁效果及声音控制等,通过具体代码示例展示了如何在虚拟现实中实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

——看一下当时的笔记表示:我也是做过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;
       }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值