GetBankAngle() public static method

public static GetBankAngle ( Vector3 tangent, Vector3 curvature, float maxBank ) : float
tangent UnityEngine.Vector3
curvature UnityEngine.Vector3
maxBank float
return float
Example #1
0
        public void MoveAlongCurve(float time, float speed, out Vector3 position, out Quaternion rotation)
        {
            position = _prevPosition;
            rotation = _prevRotation;

            if (Curve == null)
            {
                Debug.LogError("No assigned Path to follow!");
                return;
            }

            _positionAlongCurve += speed * (time - _prevTime); // alows for variable speed but involves deltas (cant be determined at a time with a single call)
            var linearTime = GetLinearTimeOnCurve(_positionAlongCurve);

            if (linearTime >= 1f)
            {
                Evt_ReachedEnd(_prevTime + (_positionAlongCurve - Curve.Length) / speed); // send up time at which end was reached
                return;
            }

            // Update pos
            Vector3 tangent;
            Vector3 curvature;

            position = Curve.Evaluate(linearTime, out tangent, out curvature);

            float bankAngle = 0f;

            if (_maxBankAngle > 0 && _bankSmoothing > 0f)
            {
                // Compute Bank
                var targetBankAngle = Curve.GetBankAngle(tangent, curvature, _maxBankAngle);
                bankAngle      = Mathf.Lerp(_prevBankAngle, targetBankAngle, Time.deltaTime / _bankSmoothing);
                _prevBankAngle = bankAngle;
            }

            if (_maxDrift > 0 && speed > 0)
            {
                // Compute Drift
                var futurePosOnCurveDist = _positionAlongCurve + (speed * _maxDrift);
                futurePosOnCurveDist = Mathf.Clamp(futurePosOnCurveDist, 0f, Curve.Length);
                var futurePosOnCurve = Curve.Evaluate(GetLinearTimeOnCurve(futurePosOnCurveDist), out tangent, out curvature);

                Debug.DrawLine(position, futurePosOnCurve);
                rotation = Quaternion.LookRotation((futurePosOnCurve - position).normalized);
            }
            else if (tangent.normalized != Vector3.zero)
            {
                rotation = Quaternion.LookRotation(tangent.normalized);
            }

            if (bankAngle != 0)
            {
                rotation *= Quaternion.AngleAxis(bankAngle, Vector3.forward);
            }

            Debug.Log(Vector3.Distance(_prevPosition, position));
            _prevPosition = position;
            _prevRotation = rotation;
            _prevTime     = time;
        }