快速开发实用工具VRTK-011

011_CameraRig_HeadSetCollisionFading 穿墙检测

防穿墙功能,穿墙就变黑(自定义颜色)

新建一个EmptyObject 命名为PlayArea ,并挂载相应的脚本。

核心脚本 VRTK_HeadsetCollision:检测碰撞,同时定义了HeadsetCollisionDetect和HeadsetCollisionEnded事件。

核心脚本 VRTK_HeadsetFade: 定义了阴影效果的实现。阴影的开始完成;阴影的褪去完成。HeadsetFadeStart,HeadsetFadeComplete,HeadsetUnfadeStart和HeadsetUnfadeComplete。

核心脚本VRTK_Headset Collision Fade:

探测什么时候用户的VR头盔与其他游戏对象发生碰撞,并且在探测到碰撞的时候淡入屏幕到一个单色。这样是为了处理那些把头部放进游戏对象(例如墙体、门、柜子)中进行透视的情况,但我们并不希望玩家发现这样的穿帮。

头盔与游戏对象发生碰撞时发出OnHeadsetCollisionDetect事件,表示探测到头盔碰撞;停止碰撞时,发出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>
        /// 发生碰撞
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnHeadsetCollisionDetect(object sender, HeadsetCollisionEventArgs e)
        {
            Invoke("StartFade", timeTillFade);
        }
        /// <summary>
        /// 碰撞结束
        /// </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);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值