Unity+Pico+SenseGlove Nova开发过程中的知识点

1、摇杆始终在视角右下角固定位置

        Vector3 horForward = playerCamera.forward;
        horForward.y = 0;
        Vector3 rightDir = Quaternion.AngleAxis(xAngle, Vector3.up) * horForward * zDistance;
        Vector3 pos = playerCamera.position + rightDir + Vector3.down * yDistance;
        rocker.position = pos;
        rocker.forward = horForward;

2、手通过摇杆控制体验者上升或下降时,达到限定高度抖动解决方法


    /// 
    /// 控制体验者上升和下降
    /// 
    /// 
    private void PlayerRise(Vector3 targetPosition)
    {
        Transform baseTransform = this.MyTransform;
        baseTransform.localEulerAngles = Vector3.zero;//摇杆前后归位

        CommonFunc.SetTrs(tipPanel, false);
        heightOffset = targetPosition.y - baseTransform.position.y;
        if (heightOffset > 0.03f)//0.03防止摇杆抖动--
        {
            rockerBase.localPosition = new Vector3(rockerBase.localPosition.x, 0.04f, rockerBase.localPosition.z);
            var vertical = player.up * Time.deltaTime * riseSpeed;
            var pos = player.position + vertical;
            if (pos.y > maxHeight)//防止体验者抖动
            {
                var y = Mathf.Max(maxHeight - pos.y, 0);
                Debug.LogError($"Max y=" + y);
                vertical = player.up * y;
                SetTipPanelMsg(true, Const.TipMaxHeight);
            }
            characterController.Move(vertical);
            Debug.LogError($"位置=" + pos + "上升=" + vertical + "maxHeight=" + maxHeight);
            if (debugText) debugText.text = "位置=" + pos + "\n上升=" + vertical + "\nmaxHeight=" + maxHeight;
        }
        else if (heightOffset < 0)
        {
            rockerBase.localPosition = new Vector3(rockerBase.localPosition.x, 0.015f, rockerBase.localPosition.z);
            var vertical = -player.up * Time.deltaTime * riseSpeed;
            var pos = player.position + vertical;
            if (pos.y < minHeight)//防止体验者抖动
            {
                var y = Mathf.Min(minHeight - pos.y, 0);
                vertical = player.up * y;
                Debug.LogError($"Min y=" + y);
                SetTipPanelMsg(true, Const.TipOnGround);
            }
            characterController.Move(vertical);
            Debug.LogError($"位置=" + pos + "下降=" + vertical + "minHeight=" + minHeight);
            if (debugText) debugText.text = "位置=" + pos + "\n下降=" + vertical + "\nminHeight=" + minHeight;
        }

        Debug.LogFormat("targetPosition.y={0}, baseTransform.Position.y={1}, heightOffset={2}, position.y={3}, ", targetPosition.y, baseTransform.position.y, heightOffset, player.position.y);
    }
   

3、当VR头盔左右旋转后实现摇杆方向始终往正前方推

    /// 
    /// 通过计算旋转偏移量控制摇杆旋转
    /// 
    /// 
    private void RockerRotation(Vector3 targetPosition)
    {
        rockerBase.localPosition = new Vector3(rockerBase.localPosition.x, 0.03f, rockerBase.localPosition.z);//摇杆上下归位
        Transform baseTransform = this.MyTransform;
        curDir = (targetPosition - rockerBase.position).normalized;
        if (lastDir == Vector3.zero)
        {
            lastDir = rockerBase.up;
        }
        angleOffset = Vector3.Angle(curDir, rockerBase.up);
        rotatedAndle = angleOffset;
        rotatedAndle *= Vector3.Angle(curDir, rockerBase.forward) < 90 ? 1 : -1;

        //根据旋转轴控制物体旋转
        SetEulerAngles(baseTransform, rotatedAndle);
        if (rotatedAndle > maxAngle)
        {
            SetEulerAngles(baseTransform, maxAngle);
            PlayerMove();
        }
        if (rotatedAndle < minAngle)
        {
            SetEulerAngles(baseTransform, minAngle);
        }
        //Debug.LogFormat("angleOffset={0},rotatedAndle={1}", angleOffset, rotatedAndle);
    }


    private void SetEulerAngles(Transform baseTransform, float value)
    {
        var euler = curEulerAngle;
        euler.x = value;
        euler.y = 0;
        euler.z = 0;
        baseTransform.localEulerAngles = euler;
    }

你可能感兴趣的:(unity,c#)