コード例 #1
0
        void FixedUpdate()
        {
            int increase = 0;

            if (_controller.hasDriver && _controller.isEnergyEnough(0.01f))
            {
                // 目标上升速度
                float targetYSpeed = 0;
                if (_controller.inputVertical > 0.01f)
                {
                    targetYSpeed = _controller.inputVertical * PEVCConfig.instance.submarineMaxUpSpeed;
                }
                else if (_controller.inputVertical < -0.01f)
                {
                    targetYSpeed = _controller.inputVertical * PEVCConfig.instance.submarineMaxDownSpeed;
                }

                // 判断应该加速还是减速
                float currentYSpeed = _controller.rigidbody.velocity.y;
                if (currentYSpeed < targetYSpeed - 0.3f)
                {
                    increase = 1;
                }
                else if (currentYSpeed > targetYSpeed + 0.3f)
                {
                    increase = -1;
                }
            }

            if (increase > 0)
            {
                _fillAmount = Mathf.Clamp01(_fillAmount - _waterSpeed * Time.deltaTime);
            }
            else if (increase < 0)
            {
                _fillAmount = Mathf.Clamp01(_fillAmount + _waterSpeed * Time.deltaTime);
            }

            if (_fillAmount > 0)
            {
                _controller.rigidbody.AddForce(0, _fillAmount * _maxWeight, 0);
            }

            // 消耗能量
            if (_controller.isPlayerHost && Mathf.Abs(_controller.inputVertical) > 0.01f)
            {
                _controller.ExpendEnergy(Time.deltaTime * _energyExpendSpeed);
            }
        }
コード例 #2
0
        void FixedUpdate()
        {
            float targetRotateSpeed = 0;
            float forceScale        = 0;

            if (_controller.isEnergyEnough(0.01f) && _controller.hasDriver)
            {
                switch (_forceDirection)
                {
                case Direction.Up:
                case Direction.Down:
                {
                    targetRotateSpeed  = _forceDirection == Direction.Up ? _maxRotateSpeed : -_maxRotateSpeed;
                    targetRotateSpeed *= _controller.inputVertical;
                    forceScale         = 1f;
                    break;
                }

                case Direction.Left:
                case Direction.Right:
                {
                    if (Mathf.Abs(_controller.inputX) > 0.01f)
                    {
                        if (_rotateRight)
                        {
                            targetRotateSpeed = _controller.inputX > 0 ? _maxRotateSpeed : -_maxRotateSpeed;
                        }
                        if (_rotateLeft)
                        {
                            targetRotateSpeed = _controller.inputX > 0 ? -_maxRotateSpeed : _maxRotateSpeed;
                        }
                    }
                    forceScale = 1f;
                    break;
                }

                case Direction.Forward:
                case Direction.Back:
                {
                    forceScale = Mathf.Clamp(Vector3.Dot(_controller.rigidbody.velocity,
                                                         _currentRotateSpeed > 0 ? transform.forward : -transform.forward), 0f, 20f);
                    forceScale = 1f - 0.0025f * forceScale * forceScale;

                    if (Mathf.Abs(_controller.inputX) > 0.01f)
                    {
                        if (_rotateRight)
                        {
                            targetRotateSpeed = _controller.inputX > 0 ? _maxRotateSpeed : -_maxRotateSpeed;
                            break;
                        }
                        if (_rotateLeft)
                        {
                            targetRotateSpeed = _controller.inputX > 0 ? -_maxRotateSpeed : _maxRotateSpeed;
                            break;
                        }
                    }

                    if (_forceDirection == Direction.Forward)
                    {
                        targetRotateSpeed = _controller.inputY * _maxRotateSpeed;
                    }
                    else
                    {
                        targetRotateSpeed = -_controller.inputY * _maxRotateSpeed;
                    }
                    break;
                }
                }
            }

            // 更新 Y 转速
            if (targetRotateSpeed > _currentRotateSpeed)
            {
                _currentRotateSpeed = Mathf.Min(targetRotateSpeed, _currentRotateSpeed + _accelerate * Time.deltaTime);
            }
            else
            {
                _currentRotateSpeed = Mathf.Max(targetRotateSpeed, _currentRotateSpeed - _accelerate * Time.deltaTime);
            }

            // 更新旋转角
            _currentZAngle = (_currentZAngle + _currentRotateSpeed * Time.deltaTime) % 360;
            _rotatePivot.localEulerAngles = new Vector3(0, 0, _currentZAngle);

            // 应用力
            if (forceScale > 0)
            {
                float force = _currentRotateSpeed / _maxRotateSpeed * _maxForce * forceScale;

                if (VFVoxelWater.self.IsInWater(transform.position))
                {
                    // 应用力
                    _controller.rigidbody.AddForceAtPosition(transform.forward * force * _controller.speedScale, _forcePivot.position);
                }

                // 消耗能量
                if (_controller.isPlayerHost)
                {
                    _controller.ExpendEnergy(force * Time.deltaTime * PEVCConfig.instance.boatPropellerEnergySpeed);
                }
            }
        }