Unity小工具-镜头状态变化触发器(位置、旋转)

镜头状态变化触发器

🥙功能描述

提供停止和运动两种状态触发,从moving=>stop触发stop事件,从stop=>moving触发moving事件(用到了UniRx插件)

🥩代码

		public UnityEvent<State> onStateChangeEvent = new UnityEvent<State>();
        public UnityEvent onStopEvent = new UnityEvent();
        public UnityEvent onMovingEvent = new UnityEvent();
        public enum State
        {
            none,
            stop,
            moving
        }
        State mState = State.none;
        public State CurrentState { get => mState; set => mState = value; }

        private double checkInterval=0.5f;
        private IDisposable checkDis;
        private Vector3 lastWpos;
        private Vector3 currentWpos;
        private Camera cam;
        private float movingStep = 0.1f;
        private Vector3 lastRotation;
        private Vector3 currentRotation;
        public Camera Cam
        {
            get
            {
                if (cam == null)
                {
                    cam = Camera.main;
                }
                return cam;
            }
        }


        void OnEnable()
        {
            base.MyOnEnable();
            UpdateLastParam();
            CheckUpdate();
            this.checkDis = Observable.Interval(TimeSpan.FromSeconds(checkInterval))
               .Subscribe(_ =>
               {
                   CheckUpdate();
               })
               .AddTo(this);
        }
        void OnDisable()
        {
            base.MyOnDisable();
            checkDis?.Dispose();

        }

        private void UpdateLastParam()
        {
            lastWpos = Cam.transform.position;
            lastRotation = Cam.transform.rotation.eulerAngles;
        }

        private void CheckUpdate()
        {
            UpdateCurrentParam();
            if (IsMoving()==true)
            {
                SwitchMovingState();
            }
            else
            {
                SwitchStopState();
            }
            UpdateLastParam();
        }

        private void SwitchStopState()
        {
            if (CurrentState != State.stop)
            {
                CurrentState = State.stop;
                onStateChangeEvent?.Invoke(CurrentState);
                onStopEvent?.Invoke();
                Debug.Log($"切换到:{CurrentState.ToString()}");
            }
        }

        private void SwitchMovingState()
        {
            if (CurrentState != State.moving)
            {
                CurrentState = State.moving;
                onStateChangeEvent?.Invoke(CurrentState);
                onMovingEvent?.Invoke();
                Debug.Log($"切换到:{CurrentState.ToString()}");
            }
        }

        private bool IsMoving()
        {
            var smPos = Vector3.SqrMagnitude(lastWpos - currentWpos);
            var smRotation = Vector3.SqrMagnitude(lastRotation - currentRotation);
            return smPos >= movingStep || smRotation >= movingStep;
        }

        private void UpdateCurrentParam()
        {
            currentWpos = Cam.transform.position;
            currentRotation = Cam.transform.rotation.eulerAngles;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牙膏上的小苏打2333

哟,哟,切克闹,煎饼果子来一套

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值