protected virtual void LateUpdate()
        {
            // Cache
            var finalTransform = FinalTransform;

            // Update position and delta
            var currentPosition = finalTransform.position;

            if (Position == PositionType.PreviousPosition && Vector3.Distance(previousPosition, currentPosition) > Threshold)
            {
                SetDelta(currentPosition - previousPosition);

                previousPosition = currentPosition;
            }

            // Update rotation
            var currentRotation = finalTransform.localRotation;
            var factor          = LeanHelper.DampenFactor(Damping, Time.deltaTime);

            if (previousDelta.sqrMagnitude > 0.0f)
            {
                UpdateRotation(finalTransform, previousDelta);
            }

            finalTransform.localRotation = Quaternion.Slerp(currentRotation, finalTransform.localRotation, factor);
        }
Ejemplo n.º 2
0
        protected virtual void LateUpdate()
        {
            if (disableWith != null && disableWith.Dragging == true)
            {
                return;
            }

            var anchoredPosition = cachedRectTransform.anchoredPosition;
            var rect             = cachedRectTransform.rect;

            if (horizontal == true && horizontalInterval != 0.0f)
            {
                var target = Mathf.Round((anchoredPosition.x - horizontalOffset) / horizontalInterval) * horizontalInterval + horizontalOffset;
                var factor = LeanHelper.DampenFactor(horizontalSpeed, Time.deltaTime);

                anchoredPosition.x = Mathf.Lerp(anchoredPosition.x, target, factor);
            }

            if (vertical == true && verticalInterval != 0.0f)
            {
                var target = Mathf.Round((anchoredPosition.y - verticalOffset) / verticalInterval) * verticalInterval + verticalOffset;
                var factor = LeanHelper.DampenFactor(verticalSpeed, Time.deltaTime);

                anchoredPosition.y = Mathf.Lerp(anchoredPosition.y, target, factor);
            }

            cachedRectTransform.anchoredPosition = anchoredPosition;
        }
Ejemplo n.º 3
0
        protected virtual void LateUpdate()
        {
            if (disableWith != null && disableWith.Dragging == true)
            {
                return;
            }

            var anchoredPosition = cachedRectTransform.anchoredPosition;
            var rect             = cachedRectTransform.rect;
            var parentSize       = ParentSize;
            var intervalX        = horizontalIntervalPixel + horizontalIntervalRect * rect.width + horizontalIntervalParent * parentSize.x;
            var intervalY        = verticalIntervalPixel + verticalIntervalRect * rect.width + verticalIntervalParent * parentSize.y;

            if (horizontal == true && intervalX != 0.0f)
            {
                var target = Mathf.Round((anchoredPosition.x - horizontalOffset) / intervalX) * intervalX + horizontalOffset;
                var factor = LeanHelper.DampenFactor(horizontalSpeed, Time.deltaTime);

                anchoredPosition.x = Mathf.Lerp(anchoredPosition.x, target, factor);
            }

            if (vertical == true && intervalY != 0.0f)
            {
                var target = Mathf.Round((anchoredPosition.y - verticalOffset) / intervalY) * intervalY + verticalOffset;
                var factor = LeanHelper.DampenFactor(verticalSpeed, Time.deltaTime);

                anchoredPosition.y = Mathf.Lerp(anchoredPosition.y, target, factor);
            }

            cachedRectTransform.anchoredPosition = anchoredPosition;
        }
        protected virtual void LateUpdate()
        {
            var currentPosition = transform.position;
            var newVector       = (Vector2)(currentPosition - previousPosition);

            if (newVector.sqrMagnitude > 0.0f)
            {
                vector = newVector;
            }

            var currentRotation = transform.localRotation;
            var factor          = LeanHelper.DampenFactor(Damping, Time.deltaTime);

            if (vector.sqrMagnitude > 0.0f)
            {
                var angle           = Mathf.Atan2(vector.x, vector.y) * Mathf.Rad2Deg;
                var directionB      = (Vector2)transform.up;
                var angleB          = Mathf.Atan2(directionB.x, directionB.y) * Mathf.Rad2Deg;
                var delta           = Mathf.DeltaAngle(angle, angleB);
                var angularVelocity = delta / Time.fixedDeltaTime;

                angularVelocity *= LeanTouch.GetDampenFactor(Damping, Time.fixedDeltaTime);

                cachedRigidbody2D.angularVelocity = angularVelocity;
            }

            transform.localRotation = Quaternion.Slerp(currentRotation, transform.localRotation, factor);

            previousPosition = currentPosition;
        }
Ejemplo n.º 5
0
        protected virtual void Update()
        {
            var factor   = LeanHelper.DampenFactor(dampening, Time.deltaTime);
            var position = default(Vector3);
            var rotation = default(Vector3);

            strength = Mathf.Lerp(strength, 0.0f, factor);
            strength = Mathf.MoveTowards(strength, 0.0f, reduction * Time.deltaTime);

            position.x = Sample(ref offsetPosition.x, 29.0f) * shakePosition.x;
            position.y = Sample(ref offsetPosition.y, 31.0f) * shakePosition.y;
            position.z = Sample(ref offsetPosition.z, 37.0f) * shakePosition.z;

            rotation.x = Sample(ref offsetRotation.x, 41.0f) * shakeRotation.x;
            rotation.y = Sample(ref offsetRotation.y, 43.0f) * shakeRotation.y;
            rotation.z = Sample(ref offsetRotation.z, 47.9f) * shakeRotation.z;

            var finalTransform     = transform;
            var finalRectTransform = transform as RectTransform;

            // Rotation
            var currentRotation = finalTransform.localRotation;

            if (currentRotation != expectedRotation)
            {
                localRotation = currentRotation;
            }

            finalTransform.localRotation = expectedRotation = localRotation * Quaternion.Euler(rotation * strength * multiplier);

            // Position
            if (finalRectTransform != null)
            {
                var currentPosition = finalRectTransform.anchoredPosition3D;

                if (currentPosition != expectedPosition)
                {
                    localPosition = currentPosition;
                }

                finalRectTransform.anchoredPosition3D = expectedPosition = localPosition + position * strength * multiplier;
            }
            else
            {
                var currentPosition = finalTransform.localPosition;

                if (currentPosition != expectedPosition)
                {
                    localPosition = currentPosition;
                }

                finalTransform.localPosition = expectedPosition = localPosition + position * strength * multiplier;
            }
        }
Ejemplo n.º 6
0
        private void MagnetPosition(ref Vector2 anchoredPosition)
        {
#if UNITY_EDITOR
            if (Application.isPlaying == false)
            {
                return;
            }
#endif
            if (dragging == false)
            {
                if (horizontal == true && horizontalClamp == true && horizontalMagnet >= 0.0f)
                {
                    var factor    = LeanHelper.DampenFactor(horizontalMagnet, Time.deltaTime);
                    var middle    = (horizontalMin + horizontalMax) * 0.5f;
                    var targetPos = horizontalMin;

                    if (anchoredPosition.x > middle)
                    {
                        targetPos = horizontalMax;
                    }

                    anchoredPosition.x = Mathf.Lerp(anchoredPosition.x, targetPos, factor);

                    target.anchoredPosition = anchoredPosition;
                }

                if (vertical == true && verticalClamp == true && verticalMagnet >= 0.0f)
                {
                    var factor    = LeanHelper.DampenFactor(verticalMagnet, Time.deltaTime);
                    var middle    = (verticalMin + verticalMax) * 0.5f;
                    var targetPos = verticalMin;

                    if (anchoredPosition.y > middle)
                    {
                        targetPos = verticalMax;
                    }

                    anchoredPosition.y = Mathf.Lerp(anchoredPosition.y, targetPos, factor);

                    target.anchoredPosition = anchoredPosition;
                }
            }
        }
        protected virtual void LateUpdate()
        {
            var currentPosition = transform.position;
            var delta           = currentPosition - previousPosition;

            if (delta.sqrMagnitude > 0.0f)
            {
                previousDelta = delta;
            }

            var currentRotation = transform.localRotation;
            var factor          = LeanHelper.DampenFactor(Dampening, Time.deltaTime);

            if (previousDelta.sqrMagnitude > 0.0f)
            {
                UpdateRotation(previousDelta);
            }

            transform.localRotation = Quaternion.Slerp(currentRotation, transform.localRotation, factor);

            previousPosition = currentPosition;
        }
Ejemplo n.º 8
0
        protected virtual void LateUpdate()
        {
            // Update position and delta
            var currentPosition = transform.position;

            if (Position == PositionType.PreviousPosition)
            {
                SetDelta(currentPosition - previousPosition);
            }

            previousPosition = currentPosition;

            // Update rotation
            var currentRotation = transform.localRotation;
            var factor          = LeanHelper.DampenFactor(Dampening, Time.deltaTime);

            if (previousDelta.sqrMagnitude > 0.0f)
            {
                UpdateRotation(previousDelta);
            }

            transform.localRotation = Quaternion.Slerp(currentRotation, transform.localRotation, factor);
        }
Ejemplo n.º 9
0
        protected virtual void Update()
        {
            var value = Vector2.zero;

            if (pointer != null)
            {
                if (IsInteractable() == true)
                {
                    if (RectTransformUtility.ScreenPointToLocalPointInRectangle(CachedRectTransform, pointer.position, pointer.pressEventCamera, out value) == true)
                    {
                        value -= offset;

                        // Clamp value
                        if (shape == ShapeType.Box)
                        {
                            value.x = Mathf.Clamp(value.x, -size.x, size.x);
                            value.y = Mathf.Clamp(value.y, -size.y, size.y);
                        }
                        else if (shape == ShapeType.Circle)
                        {
                            if (value.sqrMagnitude > radius * radius)
                            {
                                value = value.normalized * radius;
                            }
                        }
                        else if (shape == ShapeType.CircleEdge)
                        {
                            value = value.normalized * radius;
                        }
                    }
                }
                else
                {
                    NullPointerNow();
                }
            }

            // Update scaledValue
            if (shape == ShapeType.Box)
            {
                scaledValue.x = size.x > 0.0f ? value.x / size.x : 0.0f;
                scaledValue.y = size.y > 0.0f ? value.y / size.y : 0.0f;
            }
            else if (shape == ShapeType.Circle)
            {
                scaledValue = radius > 0.0f ? value / radius : Vector2.zero;
            }
            else if (shape == ShapeType.CircleEdge)
            {
                scaledValue = value.normalized;
            }

            // Update handle position
            if (handle != null)
            {
                var anchoredPosition = handle.anchoredPosition;
                var factor           = LeanHelper.DampenFactor(dampening, Time.deltaTime);

                if (snapWhileHeld == true && pointer != null)
                {
                    factor = 1.0f;
                }

                anchoredPosition = Vector2.Lerp(anchoredPosition, value + offset, factor);

                handle.anchoredPosition = anchoredPosition;
            }

            // Update relative position
            if (relativeToOrigin == true && relativeRect != null)
            {
                relativeRect.anchoredPosition = offset;
            }

            // Fire event
            if (onSet != null)
            {
                onSet.Invoke(ScaledValue);
            }
        }