快速开发实用工具VRTK-011
011_CameraRig_HeadSetCollisionFading 穿墻檢測(cè)
防穿墻功能,穿墻就變黑(自定義顏色)
新建一個(gè)EmptyObject 命名為PlayArea ,并掛載相應(yīng)的腳本。
核心腳本 VRTK_HeadsetCollision:檢測(cè)碰撞,同時(shí)定義了HeadsetCollisionDetect和HeadsetCollisionEnded事件。
核心腳本 VRTK_HeadsetFade: 定義了陰影效果的實(shí)現(xiàn)。陰影的開始完成;陰影的褪去完成。HeadsetFadeStart,HeadsetFadeComplete,HeadsetUnfadeStart和HeadsetUnfadeComplete。
核心腳本VRTK_Headset Collision Fade:
探測(cè)什么時(shí)候用戶的VR頭盔與其他游戲?qū)ο蟀l(fā)生碰撞,并且在探測(cè)到碰撞的時(shí)候淡入屏幕到一個(gè)單色。這樣是為了處理那些把頭部放進(jìn)游戲?qū)ο?#xff08;例如墻體、門、柜子)中進(jìn)行透視的情況,但我們并不希望玩家發(fā)現(xiàn)這樣的穿幫。
頭盔與游戲?qū)ο蟀l(fā)生碰撞時(shí)發(fā)出OnHeadsetCollisionDetect事件,表示探測(cè)到頭盔碰撞;停止碰撞時(shí),發(fā)出OnHeadsetCollisionEnded事件。
// Headset Collision Fade|Presence|70030 namespace VRTK {using UnityEngine;/// <summary>/// The purpose of the Headset Collision Fade is to detect when the user's VR headset collides with another game object and fades the screen to a solid colour./// </summary>/// <remarks>/// This is to deal with a user putting their head into a game object and seeing the inside of the object clipping, which is an undesired effect. The reasoning behind this is if the user puts their head where it shouldn't be, then fading to a colour (e.g. black) will make the user realise they've done something wrong and they'll probably naturally step backwards.////// The Headset Collision Fade uses a composition of the Headset Collision and Headset Fade scripts to derive the desired behaviour./// </remarks>/// <example>/// `VRTK/Examples/011_Camera_HeadSetCollisionFading` has collidable walls around the play area and if the user puts their head into any of the walls then the headset will fade to black./// </example>[RequireComponent(typeof(VRTK_HeadsetCollision)), RequireComponent(typeof(VRTK_HeadsetFade))][AddComponentMenu("VRTK/Scripts/Presence/VRTK_HeadsetCollisionFade")]public class VRTK_HeadsetCollisionFade : MonoBehaviour{[Header("Collision Fade Settings")][Tooltip("The amount of time to wait until a fade occurs.")]public float timeTillFade = 0f;[Tooltip("The fade blink speed on collision.")]public float blinkTransitionSpeed = 0.1f;[Tooltip("The colour to fade the headset to on collision.")]public Color fadeColor = Color.black;[Header("Custom Settings")][Tooltip("The VRTK Headset Collision script to use when determining headset collisions. If this is left blank then the script will need to be applied to the same GameObject.")]public VRTK_HeadsetCollision headsetCollision;[Tooltip("The VRTK Headset Fade script to use when fading the headset. If this is left blank then the script will need to be applied to the same GameObject.")]public VRTK_HeadsetFade headsetFade;protected virtual void OnEnable(){headsetFade = (headsetFade != null ? headsetFade : GetComponentInChildren<VRTK_HeadsetFade>());headsetCollision = (headsetCollision != null ? headsetCollision : GetComponentInChildren<VRTK_HeadsetCollision>());headsetCollision.HeadsetCollisionDetect += new HeadsetCollisionEventHandler(OnHeadsetCollisionDetect);headsetCollision.HeadsetCollisionEnded += new HeadsetCollisionEventHandler(OnHeadsetCollisionEnded);}protected virtual void OnDisable(){headsetCollision.HeadsetCollisionDetect -= new HeadsetCollisionEventHandler(OnHeadsetCollisionDetect);headsetCollision.HeadsetCollisionEnded -= new HeadsetCollisionEventHandler(OnHeadsetCollisionEnded);}/// <summary>/// 發(fā)生碰撞/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected virtual void OnHeadsetCollisionDetect(object sender, HeadsetCollisionEventArgs e){Invoke("StartFade", timeTillFade);}/// <summary>/// 碰撞結(jié)束/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected virtual void OnHeadsetCollisionEnded(object sender, HeadsetCollisionEventArgs e){CancelInvoke("StartFade");headsetFade.Unfade(blinkTransitionSpeed);}/// <summary>/// 陰影效果/// </summary>protected virtual void StartFade(){headsetFade.Fade(fadeColor, blinkTransitionSpeed);}} }?
總結(jié)
以上是生活随笔為你收集整理的快速开发实用工具VRTK-011的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蒙特卡洛近似的一些例子
- 下一篇: OpenDRIVE坐标系解读