Example #1
0
            public void DoAxisLimit(ref AxisState axis, Vector3 up, bool isYAxis = false, float fov = 0)
            {
                if (m_LimitMode == LimitMode.None)
                {
                    return;
                }

                if (!m_Enable)
                {
                    return;
                }

                if (m_RemoveLimitMode == RemoveLimitMode.Once)
                {
                    float input = Mathf.Abs(CinemachineCore.GetInputAxis("RightJoystickVertical"));
                    input += Mathf.Abs(CinemachineCore.GetInputAxis("RightJoystickHorizontal"));
                    if (input != 0)
                    {
                        m_Enable = false;
                    }
                }



                if (m_LimitMode == LimitMode.SceneLook)
                {
                    if (m_LookTarget == null || m_Camera == null)
                    {
                        return;
                    }

                    Vector3 dir = m_LookTarget.position - m_Camera.position;
                    if (!isYAxis)
                    {
                        dir.y = 0;
                    }
                    dir = dir.normalized;
                    float   angle  = Vector3.Angle(m_AxisVector, dir);
                    Vector3 normal = Vector3.Cross(m_AxisVector, dir);
                    angle *= Mathf.Sign(Vector3.Dot(normal, up));

                    if (isYAxis)
                    {
                        angle = (angle - (90 - fov)) / (fov * 2);
                    }
                    else
                    {
                        if (!axis.ValueRangeLocked)
                        {
                            axis.m_MaxValue = angle + m_LimitRange;
                            axis.m_MinValue = angle - m_LimitRange;
                        }
                    }

                    axis.Value = angle;
                }
            }
Example #2
0
        /// <summary>
        /// Updates the state of this axis based on the axis defined
        /// by AxisState.m_AxisName
        /// </summary>
        /// <param name="deltaTime">Delta time in seconds</param>
        /// <returns>Returns <b>true</b> if this axis' input was non-zero this Update,
        /// <b>false</b> otherwise</returns>
        public bool Update(float deltaTime)
        {
            if (!string.IsNullOrEmpty(m_InputAxisName))
            {
                try { m_InputAxisValue = CinemachineCore.GetInputAxis(m_InputAxisName); }
                catch (ArgumentException e) { Debug.LogError(e.ToString()); }
            }

            float input = m_InputAxisValue;

            if (m_InvertInput)
            {
                input *= -1f;
            }

            if (m_SpeedMode == SpeedMode.MaxSpeed)
            {
                return(MaxSpeedUpdate(input, deltaTime)); // legacy mode
            }
            // Direct mode update: maxSpeed interpreted as multiplier
            input *= m_MaxSpeed;
            if (deltaTime < Epsilon)
            {
                mCurrentSpeed = 0;
            }
            else
            {
                float speed    = input / deltaTime;
                float dampTime = Mathf.Abs(speed) < Mathf.Abs(mCurrentSpeed) ? m_DecelTime : m_AccelTime;
                speed         = mCurrentSpeed + Damper.Damp(speed - mCurrentSpeed, dampTime, deltaTime);
                mCurrentSpeed = speed;

                // Decelerate to the end points of the range if not wrapping
                float range = m_MaxValue - m_MinValue;
                if (!m_Wrap && m_DecelTime > Epsilon && range > Epsilon)
                {
                    float v0 = ClampValue(Value);
                    float v  = ClampValue(v0 + speed * deltaTime);
                    float d  = (speed > 0) ? m_MaxValue - v : v - m_MinValue;
                    if (d < (0.1f * range) && Mathf.Abs(speed) > Epsilon)
                    {
                        speed = Damper.Damp(v - v0, m_DecelTime, deltaTime) / deltaTime;
                    }
                }
                input = speed * deltaTime;
            }
            Value = ClampValue(Value + input);
            return(Mathf.Abs(input) > Epsilon);
        }
        public bool Update(float deltaTime, ref AxisBase axis)
        {
            if (!string.IsNullOrEmpty(name))
            {
                try { inputValue = CinemachineCore.GetInputAxis(name); }
                catch (ArgumentException) {}
                //catch (ArgumentException e) { Debug.LogError(e.ToString()); }
            }

            float input = inputValue * multiplier;

            if (deltaTime < Epsilon)
            {
                mCurrentSpeed = 0;
            }
            else
            {
                float speed    = input / deltaTime;
                float dampTime = Mathf.Abs(speed) < Mathf.Abs(mCurrentSpeed) ? decelTime : accelTime;
                speed         = mCurrentSpeed + Damper.Damp(speed - mCurrentSpeed, dampTime, deltaTime);
                mCurrentSpeed = speed;

                // Decelerate to the end points of the range if not wrapping
                float range = axis.m_MaxValue - axis.m_MinValue;
                if (!axis.m_Wrap && decelTime > Epsilon && range > Epsilon)
                {
                    float v0 = ClampValue(ref axis, axis.m_Value);
                    float v  = ClampValue(ref axis, v0 + speed * deltaTime);
                    float d  = (speed > 0) ? axis.m_MaxValue - v : v - axis.m_MinValue;
                    if (d < (0.1f * range) && Mathf.Abs(speed) > Epsilon)
                    {
                        speed = Damper.Damp(v - v0, decelTime, deltaTime) / deltaTime;
                    }
                }
                input = speed * deltaTime;
            }

            axis.m_Value = ClampValue(ref axis, axis.m_Value + input);
            return(Mathf.Abs(inputValue) > Epsilon);
        }
Example #4
0
            public bool TriggerInvoke()
            {
                bool isInvoke = false;

                if (IsSelectTriggerType(TriggerMode.InputAxis))
                {
                    string[] strs  = m_TriggerInputAxisName.Split(';');
                    float    input = 0;
                    for (int i = 0; i < strs.Length; i++)
                    {
                        input += Mathf.Abs(CinemachineCore.GetInputAxis(strs[i]));
                    }
                    if (input != 0)
                    {
                        isInvoke = true;
                    }
                }

                if (!isInvoke && IsSelectTriggerType(TriggerMode.InputButton))
                {
                    string[] strs  = m_TriggerInputButtonName.Split(';');
                    float    input = 0;
                    for (int i = 0; i < strs.Length; i++)
                    {
                        input += Mathf.Abs(CinemachineCore.GetInputAxis(strs[i]));
                    }
                    if (input != 0)
                    {
                        isInvoke = true;
                    }
                }

                if (isInvoke)
                {
                    Invoke();
                }
                return(isInvoke);
            }
Example #5
0
        /// <summary>
        /// Updates the state of this axis based on the axis defined
        /// by AxisState.m_AxisName
        /// </summary>
        /// <param name="deltaTime">Delta time in seconds</param>
        /// <returns>Returns <b>true</b> if this axis' Input was non-zero this Update,
        /// <b>flase</b> otherwise</returns>
        public bool Update(float deltaTime)
        {
            if (!string.IsNullOrEmpty(m_InputAxisName))
            {
                try
                {
                    m_InputAxisValue = CinemachineCore.GetInputAxis(m_InputAxisName);
                }
                catch (ArgumentException e)
                {
                    Debug.LogError(e.ToString());
                }
            }

            float input = m_InputAxisValue;

            if (m_InvertAxis)
            {
                input *= -1f;
            }

            if (m_MaxSpeed > Epsilon)
            {
                float targetSpeed = input * m_MaxSpeed;
                if (Mathf.Abs(targetSpeed) < Epsilon ||
                    (Mathf.Sign(mCurrentSpeed) == Mathf.Sign(targetSpeed) &&
                     Mathf.Abs(targetSpeed) < Mathf.Abs(mCurrentSpeed)))
                {
                    // Need to decelerate
                    float a     = Mathf.Abs(targetSpeed - mCurrentSpeed) / Mathf.Max(Epsilon, m_DecelTime);
                    float delta = Mathf.Min(a * deltaTime, Mathf.Abs(mCurrentSpeed));
                    mCurrentSpeed -= Mathf.Sign(mCurrentSpeed) * delta;
                }
                else
                {
                    // Accelerate to the target speed
                    float a = Mathf.Abs(targetSpeed - mCurrentSpeed) / Mathf.Max(Epsilon, m_AccelTime);
                    mCurrentSpeed += Mathf.Sign(targetSpeed) * a * deltaTime;
                    if (Mathf.Sign(mCurrentSpeed) == Mathf.Sign(targetSpeed) &&
                        Mathf.Abs(mCurrentSpeed) > Mathf.Abs(targetSpeed))
                    {
                        mCurrentSpeed = targetSpeed;
                    }
                }
            }

            // Clamp our max speeds so we don't go crazy
            float maxSpeed = GetMaxSpeed();

            mCurrentSpeed = Mathf.Clamp(mCurrentSpeed, -maxSpeed, maxSpeed);

            Value += mCurrentSpeed * deltaTime;
            bool isOutOfRange = (Value > mMaxValue) || (Value < mMinValue);

            if (isOutOfRange)
            {
                if (mWrapAround)
                {
                    if (Value > mMaxValue)
                    {
                        Value = mMinValue + (Value - mMaxValue);
                    }
                    else
                    {
                        Value = mMaxValue + (Value - mMinValue);
                    }
                }
                else
                {
                    Value         = Mathf.Clamp(Value, mMinValue, mMaxValue);
                    mCurrentSpeed = 0f;
                }
            }
            return(Mathf.Abs(input) > Epsilon);
        }
Example #6
0
        public bool Update(float deltaTime)
        {
            if (!string.IsNullOrEmpty(this.m_InputAxisName))
            {
                try
                {
                    this.m_InputAxisValue = CinemachineCore.GetInputAxis(this.m_InputAxisName);
                }
                catch (ArgumentException ex)
                {
                    Debug.LogError(ex.ToString());
                }
            }
            float num = this.m_InputAxisValue;

            if (this.m_InvertAxis)
            {
                num *= -1f;
            }
            if (this.m_MaxSpeed > 0.0001f)
            {
                float num2 = num * this.m_MaxSpeed;
                if (Mathf.Abs(num2) < 0.0001f || (Mathf.Sign(this.mCurrentSpeed) == Mathf.Sign(num2) && Mathf.Abs(num2) < Mathf.Abs(this.mCurrentSpeed)))
                {
                    float num3 = Mathf.Min(Mathf.Abs(num2 - this.mCurrentSpeed) / Mathf.Max(0.0001f, this.m_DecelTime) * deltaTime, Mathf.Abs(this.mCurrentSpeed));
                    this.mCurrentSpeed -= Mathf.Sign(this.mCurrentSpeed) * num3;
                }
                else
                {
                    float num4 = Mathf.Abs(num2 - this.mCurrentSpeed) / Mathf.Max(0.0001f, this.m_AccelTime);
                    this.mCurrentSpeed += Mathf.Sign(num2) * num4 * deltaTime;
                    if (Mathf.Sign(this.mCurrentSpeed) == Mathf.Sign(num2) && Mathf.Abs(this.mCurrentSpeed) > Mathf.Abs(num2))
                    {
                        this.mCurrentSpeed = num2;
                    }
                }
            }
            float maxSpeed = this.GetMaxSpeed();

            this.mCurrentSpeed = Mathf.Clamp(this.mCurrentSpeed, -maxSpeed, maxSpeed);
            this.Value        += this.mCurrentSpeed * deltaTime;
            if (this.Value > this.mMaxValue || this.Value < this.mMinValue)
            {
                if (this.mWrapAround)
                {
                    if (this.Value > this.mMaxValue)
                    {
                        this.Value = this.mMinValue + (this.Value - this.mMaxValue);
                    }
                    else
                    {
                        this.Value = this.mMaxValue + (this.Value - this.mMinValue);
                    }
                }
                else
                {
                    this.Value         = Mathf.Clamp(this.Value, this.mMinValue, this.mMaxValue);
                    this.mCurrentSpeed = 0f;
                }
            }
            return(Mathf.Abs(num) > 0.0001f);
        }
Example #7
0
            /// <summary>
            /// Updates the state of this axis based on the axis defined
            /// by <see cref="AxisState.m_AxisName"/>
            /// </summary>
            /// <param name="dt">Delta time in seconds</param>
            /// <param name="invertAxisInput">If <b>TRUE</b>, inverts the value of the axis.
            /// Otherwise, the value is not modified</param>
            /// <return>Returns <b>TRUE</b> if this axis' input was non-zero this Update,
            /// <b>FALSE</b> otherwise</return>
            public bool Update(float dt, bool invertAxisInput)
            {
                if (!string.IsNullOrEmpty(m_InputAxisName))
                {
                    try
                    {
                        m_InputAxisValue = CinemachineCore.GetInputAxis(m_InputAxisName);
                    }
                    catch (ArgumentException e)
                    {
                        Debug.LogError(e.ToString());
                    }
                }

                float input = m_InputAxisValue;

                if (invertAxisInput)
                {
                    input *= -1f;
                }

                float absInput    = Mathf.Abs(input);
                bool  axisNonZero = absInput > UnityVectorExtensions.Epsilon;

                // Test to see if we're commanding a speed faster than we are going
                float accelTime = Mathf.Max(0.001f, m_AccelTime);

                if (axisNonZero && (absInput >= Mathf.Abs(mCurrentSpeed / m_MaxSpeed)))
                {
                    if (m_MaxSpeed > UnityVectorExtensions.Epsilon)
                    {
                        mCurrentSpeed += ((m_MaxSpeed / accelTime) * input) * dt;
                    }
                }
                else
                {
                    // Otherwise brake
                    // TODO: Can the fluctuation between these two cause nasty behaviour? Must monitor..
                    float decelTime = Mathf.Max(0.001f, m_DecelTime);
                    float reduction = Mathf.Sign(mCurrentSpeed) * (m_MaxSpeed / decelTime) * dt;
                    mCurrentSpeed = (Mathf.Abs(reduction) >= Mathf.Abs(mCurrentSpeed))
                        ? 0f : (mCurrentSpeed - reduction);
                }

                // Clamp our max speeds so we don't go crazy
                float maxSpeed = GetMaxSpeed();

                mCurrentSpeed = Mathf.Clamp(mCurrentSpeed, -maxSpeed, maxSpeed);

                Value += mCurrentSpeed * dt;
                bool isOutOfRange = (Value > mMaxValue) || (Value < mMinValue);

                if (isOutOfRange)
                {
                    if (mWrapAround)
                    {
                        if (Value > mMaxValue)
                        {
                            Value = mMinValue + (Value - mMaxValue);
                        }
                        else
                        {
                            Value = mMaxValue + (Value - mMinValue);
                        }
                    }
                    else
                    {
                        Value         = Mathf.Clamp(Value, mMinValue, mMaxValue);
                        mCurrentSpeed = 0f;
                    }
                }
                return(axisNonZero);
            }
Example #8
0
            /// <summary>Bring the axis back to the centered state (only if enabled).</summary>
            public void DoRecentering(ref AxisState axis, float deltaTime, float recenterTarget)
            {
                if (!m_enabled && deltaTime >= 0)
                {
                    return;
                }

                recenterTarget = axis.ClampValue(recenterTarget);
                if (deltaTime < 0)
                {
                    CancelRecentering();
                    if (m_enabled)
                    {
                        axis.Value = recenterTarget;
                    }
                    return;
                }


                float v     = axis.ClampValue(axis.Value);
                float delta = recenterTarget - v;

                if (delta == 0)
                {
                    return;
                }

                float input = 0;

                if (m_NeedReTimingInputAxisName != "")
                {
                    string[] names = m_NeedReTimingInputAxisName.Split(';');
                    for (int i = 0; i < names.Length; i++)
                    {
                        if (CinemachineCore.GetInputAxis(names[i]) != 0)
                        {
                            input = 1;
                            break;
                        }
                    }
                }

                if (input != 0)
                {
                    CancelRecentering();
                }

                input = 0;
                if (m_NeedRecenterInputAxisName != "")
                {
                    string[] names = m_NeedRecenterInputAxisName.Split(';');
                    for (int i = 0; i < names.Length; i++)
                    {
                        if (CinemachineCore.GetInputAxis(names[i]) != 0)
                        {
                            input = 1;
                            break;
                        }
                    }
                }

                if (Time.time < (mLastAxisInputTime + m_WaitTime) && input == 0)
                {
                    return;
                }

                // Determine the direction
                float r = axis.m_MaxValue - axis.m_MinValue;

                if (axis.m_Wrap && Mathf.Abs(delta) > r * 0.5f)
                {
                    v += Mathf.Sign(recenterTarget - v) * r;
                }

                // Damp our way there
                float recenteringTime = input == 0? m_RecenteringTime : m_RecenterInputAxisTime;

                if (recenteringTime < 0.001f)
                {
                    v = recenterTarget;
                }
                else
                {
                    v = Mathf.SmoothDamp(
                        v, recenterTarget, ref mRecenteringVelocity,
                        recenteringTime, 9999, deltaTime);
                }
                axis.Value = axis.ClampValue(v);
            }