Example #1
0
    public void Update()
    {
        if (Target == null)
        {
            return;
        }
        //  ChangeDirectionByDragging();
        //if (_dragging && _directorToLeft && !_bTouchMouse)  //左遥感
        //{
        //    _rockerControl = ComMoveController.RockerControl.LeftControl;
        //    _ufpTransform.RotateAround(_playerTransformObj.position, FPVector.up, FrameSyncManager.DeltaTime * RockerSpeed);
        //    _ufpTransform.UpdateAllData();
        //    Debug.Log("_ufpTransform:::" + _ufpTransform.forward.ToVector());
        //    //transform.RotateAround(_childObj.position, Vector3.up, Time.deltaTime * RockerSpeed);
        //    return;
        //}
        //else if (_dragging && _directorToRight && !_bTouchMouse) //右遥感
        //{
        //    _rockerControl = ComMoveController.RockerControl.RightControl;
        //    _ufpTransform.RotateAround(_playerTransformObj.position, FPVector.up, FrameSyncManager.DeltaTime * -RockerSpeed);
        //    _ufpTransform.UpdateAllData();

        //    // transform.RotateAround(_childObj.position, Vector3.up, Time.deltaTime * -RockerSpeed);
        //    return;
        //}


        //Debug.Log("_currentAngle:::" + _currentAngle + ",_childObj.eulerAngles.y::" + _childObj.eulerAngles.y
        //    + ",transform.rotation::"+ transform.rotation);

        RaycastHit GroundHitInfo;
        RaycastHit BarrieHitrInfo;
        bool       bGroundInfoFlag  = _motionObj.RayGroundInfo(out GroundHitInfo);
        bool       bBarrierInfoFlag = _motionObj.RayBarrierInfo(_childTransformObj.forward.ToVector(), out BarrieHitrInfo);

        FPVector fpGroundInfoNormal = FPVector.zero;

        if (bGroundInfoFlag)
        {
            fpGroundInfoNormal = GroundHitInfo.normal.ToFPVector();
        }

        if (!_dragging && !_bTouchMouse && _bBeforeDragging)
        {
            _rockerControl = ComMoveController.RockerControl.None;
            return;
        }
        else if (!_dragging)
        {
            if (_bBeforeDragging)  //在这里设置值,为了保证childObj的forward朝向跟摄像机的一致,防止先后差值
            {
                _bBeforeDragging = false;
                return;
            }

            if (!bGroundInfoFlag)  //在空中
            {
                _rockerControl = ComMoveController.RockerControl.AirControl;
                if (_motionObj.bJumpFlag && false)  //这是处理不跟随着对象跳跃的逻辑部分
                {
                    Distance = FPMath.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseScrollWheelSensitivity), DistanceMin, DistanceMax);
                    FPVector DataVec3 = new FPVector(_playerTransformObj.position.x, transform.position.y - Height, _playerTransformObj.position.z);
                    _ufpTransform.position = (_ufpTransform.rotation * new FPVector(.0f, Height, -Distance)) + DataVec3;
                    _ufpTransform.UpdateAllData();
                    return;
                }
            }
            else if (!bBarrierInfoFlag || !_motionObj.JudgetGroundSlope(BarrieHitrInfo.normal))  //有地面接触但前方没有障碍物
            {
                if (!_motionObj.JudgetGroundSlope(GroundHitInfo.normal))
                {
                    _rockerControl  = ComMoveController.RockerControl.OtherControl;
                    _climbOtherWall = false;
                }
                else
                {
                    if (_climbOtherWall && _beforeWallNormalVec3 != fpGroundInfoNormal)  //表示从一面墙跨到另外一面墙
                    {
                        _beforeGroundNormalVec3 = _beforeWallNormalVec3;
                        _beforeWallNormalVec3   = fpGroundInfoNormal;
                    }
                    _rockerControl = ComMoveController.RockerControl.ClimbControl;
                }
            }
            else //有地面接触且前方有障碍物
            {
                _rockerControl = ComMoveController.RockerControl.None;
                _beforeControl = ComMoveController.RockerControl.OtherControl;
                if (!_motionObj.JudgetGroundSlope(GroundHitInfo.normal)) //从地面跨到墙上的情况
                {
                    _beforeGroundNormalVec3 = fpGroundInfoNormal;
                }
                else                                                   //从一面墙跨到另外一面墙的情况
                {
                    _climbOtherWall       = true;
                    _beforeWallNormalVec3 = fpGroundInfoNormal;   //设置这个变量的原因是:有可能检测到障碍物,但是玩家并没有跨越过去
                }
            }
        }

        if (_rockerControl == RockerControl.AirControl)
        {
            Distance = FPMath.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseScrollWheelSensitivity), DistanceMin, DistanceMax);
            _ufpTransform.position = (_ufpTransform.rotation * new FPVector(.0f, Height, -Distance)) + _playerTransformObj.position;
            _ufpTransform.UpdateAllData();
        }
        else if (_rockerControl == RockerControl.OtherControl)
        {
            var quaternion = FPQuaternion.AngleAxis((_currentAngle) + _childTransformObj.transform.eulerAngles.y, FPVector.up);
            Distance = FPMath.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseScrollWheelSensitivity), DistanceMin, DistanceMax);
            _ufpTransform.SetRotation(quaternion);
            // transform.rotation = quaternion;
            FPVector data = (_ufpTransform.rotation * new FPVector(.0f, Height, -Distance));

            _ufpTransform.position = (_ufpTransform.rotation * new FPVector(.0f, Height, -Distance)) + _playerTransformObj.position;
            _ufpTransform.UpdatePosition();
            //Debug.Log("data::"+ data.ToVector()+ ", _ufpTransform.position::::" + _ufpTransform.position.ToVector()
            //    + ",transform::" + transform.position + "::_playerTransformObj.position:" + _playerTransformObj.position.ToVector()
            //    + ",,name::" + _playerTransformObj.gameObject.name);
            _rotation = _ufpTransform.rotation;
            _ufpTransform.UpdateForward();
        }
        else if (_rockerControl == RockerControl.ClimbControl)
        {
            Distance = FPMath.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseScrollWheelSensitivity), DistanceMin, DistanceMax);
            var quaternion = FPQuaternion.AngleAxis((0) + transform.eulerAngles.y, FPVector.up);
            _ufpTransform.position = (_ufpTransform.rotation * new FPVector(.0f, Height, -Distance)) + _playerTransformObj.position;
            _ufpTransform.UpdateAllData();

            FPVector climbForward = _ufpTransform.forward;

            if (_beforeControl == ComMoveController.RockerControl.OtherControl && _beforeGroundNormalVec3 != FPVector.zero)
            {
                FP       tempAngle = FPVector.Angle(_beforeGroundNormalVec3, fpGroundInfoNormal);
                FPVector normal    = FPVector.Cross(_beforeGroundNormalVec3, fpGroundInfoNormal); //叉乘求出法线向量
                                                                                                  //  num *= Mathf.Sign(Vector3.Dot(normal, info.transform.up));  //求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
                climbForward          = FPQuaternion.AngleAxis((90 - tempAngle), normal) * fpGroundInfoNormal;
                climbForward          = -1 * climbForward;
                _finishWallNormalVec3 = climbForward;
                _beforeControl        = ComMoveController.RockerControl.ClimbControl;
            }

            FP forwardAngle = FPVector.Angle(_finishWallNormalVec3, _ufpTransform.forward);
            if (forwardAngle != 0 && false)  //处理摄像机角度偏转
            {
                //1)调整摄像机的旋转角度
                float    direcFlag  = -1;
                FPVector normalVec3 = FPVector.Cross(_finishWallNormalVec3, _ufpTransform.forward);           //叉乘求出法线向量
                direcFlag    *= FPMath.Sign(Vector3.Dot(normalVec3.ToVector(), _ufpTransform.up.ToVector())); //求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
                forwardAngle *= direcFlag;

                FPVector beforeForward = _ufpTransform.forward;
                FPVector forward       = FPQuaternion.AngleAxis(forwardAngle, _ufpTransform.up) * _ufpTransform.forward;
                //   Debug.Log("_ufpTransform.forward::" + _ufpTransform.forward.ToVector() + ",forward::" + forward.ToVector()
                //        + "forwardAngle:::" + forwardAngle.AsFloat() + ",forward1111::" + forward);
                float quaternionSpeed = 0.003f;
                if (!_bTouchMouse)
                {
                    quaternionSpeed = 0.03f;
                }
                if (beforeForward != forward)
                {
                    Debug.Log("LookRotation(forward):::" + FPQuaternion.LookRotation(forward) + ",_rotation::" + _rotation + ",unity::" + Quaternion.LookRotation(forward.ToVector()));

                    _rotation = FPQuaternion.Slerp(_rotation, FPQuaternion.LookRotation(forward), quaternionSpeed);
                    _ufpTransform.SetRotation(_rotation);
                }
                //   Debug.Log(",forward::"+ forward.ToVector() + ",_ufpTransform.forward::" + _ufpTransform.forward.ToVector());
                //2)调整人物的旋转角度
                if (!_climbOtherWall)  // 这是从地面爬到墙得处理,如果是从一面墙爬到另外一面墙,镜头不做转换
                {
                    Debug.Log("beforeForward:::" + beforeForward.ToVector() + ",_ufpTransform.forward::" + _ufpTransform.forward.ToVector());
                    _offsetAngle = FPVector.Angle(beforeForward, _ufpTransform.forward) * direcFlag;
                    _avatarObj.ChangeAvaterForward(_offsetAngle);
                }
            }
        }

        Debug.DrawLine(_ufpTransform.position.ToVector(), _playerTransformObj.transform.position, Color.red);

        if (_rockerControl == RockerControl.OtherControl ||
            _rockerControl == RockerControl.ClimbControl)
        {
            //看是否有障碍物
            FPVector directionTarget = (_ufpTransform.position - _ufpTransform.ChangeVec3ToTSVec(_rayPointObj.position)).normalized;
            FP       distance        = FPVector.Distance(_ufpTransform.position, _ufpTransform.ChangeVec3ToTSVec(_rayPointObj.position));
            if (distance > Distance)
            {
                _ufpTransform.Translate(directionTarget * (distance - Distance));
                _ufpTransform.UpdateRotationAndPosition();
            }

            //  Debug.DrawRay(_rayPointObj.position, directionTarget * Distance, Color.black);
            int        layerMask = LayerMask.GetMask(Layers.Render);
            RaycastHit info;
            if (Physics.Raycast(_rayPointObj.position, directionTarget.ToVector(), out info, Distance.AsFloat(), layerMask))  //如果
            {
                //   Debug.Log("info.name::" + info.transform.name);
                if (info.transform.name != transform.name /*&& info.transform.tag != Tags.Ground*/)
                {
                    _ufpTransform.SetPosition(_ufpTransform.ChangeVec3ToTSVec(info.point));
                    //transform.position = info.point;
                }
                if (_rockerControl == RockerControl.OtherControl)
                {
                    _beforeControl = RockerControl.OtherControl;
                }
            }
        }
    }