Exemplo n.º 1
0
        protected virtual void UpdatePosition(float damping, float linear)
        {
            if (positionSet == true || Continuous == true)
            {
                if (destination != null)
                {
                    Position = destination.TransformPoint(DestinationOffset);
                }

                var currentPosition = transform.position;
                var targetPosition  = Position + Offset;

                if (IgnoreZ == true)
                {
                    targetPosition.z = currentPosition.z;
                }

                // Get t value
                var factor = LeanHelper.GetDampenFactor(damping, Time.deltaTime);

                // Move current value to the target one
                currentPosition = Vector3.Lerp(currentPosition, targetPosition, factor);
                currentPosition = Vector3.MoveTowards(currentPosition, targetPosition, linear * Time.deltaTime);

                // Apply new point
                transform.position = currentPosition;

                positionSet = false;
            }
        }
        protected override void UpdatePosition(float damping, float linear)
        {
            if (positionSet == true || Continuous == true)
            {
                if (destination != null)
                {
                    Position = destination.TransformPoint(DestinationOffset);
                }

                var currentPosition = (Vector2)(transform.position);
                var targetPosition  = (Vector2)(Position + Offset);

                var direction = targetPosition - currentPosition;
                var velocity  = direction / Time.fixedDeltaTime;

                // Apply the velocity
                velocity *= LeanHelper.GetDampenFactor(damping, Time.fixedDeltaTime);
                velocity  = Vector3.MoveTowards(velocity, Vector3.zero, linear * Time.fixedDeltaTime);

                if (axis == AxisType.Horizontal)
                {
                    velocity.y = cachedRigidbody.velocity.y;
                }
                else if (axis == AxisType.Vertical)
                {
                    velocity.x = cachedRigidbody.velocity.x;
                }

                cachedRigidbody.velocity = velocity;

                fixedUpdateCalled = true;
            }
        }
        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.GetDampenFactor(Damping, Time.deltaTime);

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

            finalTransform.localRotation = Quaternion.Slerp(currentRotation, finalTransform.localRotation, factor);
        }
        protected virtual void Update()
        {
            if (reverting == true)
            {
                if (ReachedTarget() == true)
                {
                    reverting = false;

                    return;
                }

                // Get t value
                var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

                if (RevertPosition == true)
                {
                    transform.localPosition = Vector3.Lerp(transform.localPosition, TargetPosition, factor);
                }

                if (RevertRotation == true)
                {
                    transform.localRotation = Quaternion.Slerp(transform.localRotation, TargetRotation, factor);
                }

                if (RevertScale == true)
                {
                    transform.localScale = Vector3.Lerp(transform.localScale, TargetScale, factor);
                }
            }
        }
Exemplo n.º 5
0
        public void UpdateSwap()
        {
            var prefab = GetPrefab();

            if (clone != null)
            {
                if (clonePrefab == prefab)
                {
                    return;
                }

                LeanHelper.Destroy(clone.gameObject);

                clone       = null;
                clonePrefab = null;
            }

            if (Prefabs != null && Prefabs.Count > 0)
            {
                clone = Instantiate(prefab);

                clone.transform.SetParent(transform, false);

                clonePrefab = prefab;
            }
        }
Exemplo n.º 6
0
        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.GetDampenFactor(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 *= LeanHelper.GetDampenFactor(Damping, Time.fixedDeltaTime);

                cachedRigidbody2D.angularVelocity = angularVelocity;
            }

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

            previousPosition = currentPosition;
        }
        public bool TryQuery <T>(GameObject gameObject, Vector2 screenPosition, ref T result, ref Component root, ref Vector3 worldPosition)
        {
            var camera       = LeanHelper.GetCamera(Camera, gameObject);
            var bestHit      = default(Component);
            var bestDistance = float.PositiveInfinity;
            var bestPosition = default(Vector3);

            if (camera != null)
            {
                if (camera.pixelRect.Contains(screenPosition) == true)
                {
                    DoRaycast3D(camera, screenPosition, ref bestHit, ref bestDistance, ref bestPosition);
                    DoRaycast2D(camera, screenPosition, ref bestHit, ref bestDistance, ref bestPosition);
                    DoRaycastUI(screenPosition, ref bestHit, ref bestDistance, ref bestPosition);
                }
            }

            if (bestHit != null)
            {
                worldPosition = bestPosition;

                return(TryResult(bestHit, ref result, ref root));
            }

            return(false);
        }
Exemplo n.º 8
0
        protected virtual void Update()
        {
            var finalTransform    = Target != null ? Target.transform : transform;
            var factor            = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);
            var newRemainingDelta = Vector3.Lerp(remainingDelta, Vector3.zero, factor);
            var rigidbody         = finalTransform.GetComponent <Rigidbody>();

            if (rigidbody != null)
            {
                if (ResetVelocityInUpdate == true)
                {
                    rigidbody.velocity = Vector3.zero;
                }

                rigidbody.velocity += (remainingDelta - newRemainingDelta) / Time.deltaTime;
            }

            if (controlling == false && Inertia > 0.0f && Damping > 0.0f)
            {
                newRemainingDelta = Vector3.Lerp(newRemainingDelta, remainingDelta, Inertia);
            }

            remainingDelta = newRemainingDelta;
            controlling    = false;
        }
Exemplo n.º 9
0
        protected virtual void LateUpdate()
        {
            var worldOrigin    = transform.parent != null ? transform.parent.position : Vector3.zero;
            var worldDirection = Direction;

            // Get a valid normalized direction
            if (worldDirection.sqrMagnitude == 0.0f)
            {
                worldDirection = transform.position - worldOrigin;

                if (worldDirection.sqrMagnitude == 0.0f)
                {
                    worldDirection = Random.onUnitSphere;
                }
            }
            else if (DirectionSpace == Space.Self)
            {
                worldDirection = transform.TransformDirection(worldDirection);
            }

            worldDirection = worldDirection.normalized;

            // Limit distance to min/max values?
            if (Clamp == true)
            {
                Distance = Mathf.Clamp(Distance, ClampMin, ClampMax);
            }

            // Collide against stuff?
            if (CollisionLayers != 0)
            {
                var hit    = default(RaycastHit);
                var pointA = worldOrigin + worldDirection * ClampMin;
                var pointB = worldOrigin + worldDirection * ClampMax;

                if (Physics.SphereCast(pointA, CollisionRadius, worldDirection, out hit, Vector3.Distance(pointA, pointB), CollisionLayers) == true)
                {
                    var newDistance = hit.distance + ClampMin;

                    // Only update if the distance is closer, else the camera can glue to walls behind it
                    if (newDistance < Distance)
                    {
                        Distance = newDistance;
                    }
                }
            }

            // Get t value
            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            // Lerp the current value to the target one
            currentDistance = Mathf.Lerp(currentDistance, Distance, factor);

            // Set the position
            transform.position = worldOrigin + worldDirection * currentDistance;
        }
Exemplo n.º 10
0
        /// <summary>This method will automatically update the <b>Pitch</b> and <b>Yaw</b> values based on the specified position in screen space.</summary>
        public void RotateToScreenPosition(Vector2 screenPosition)
        {
            var camera = LeanHelper.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                var xyz = camera.ScreenPointToRay(screenPosition).direction;

                RotateToDirection(xyz);
            }
        }
Exemplo n.º 11
0
        protected virtual void FixedUpdate()
        {
            var finalTransform = Target != null ? Target.transform : transform;
            var factor         = LeanHelper.GetDampenFactor(Damping, Time.fixedDeltaTime);
            var newDelta       = Vector2.Lerp(remainingDelta, Vector2.zero, factor);
            var rigidbody      = finalTransform.GetComponent <Rigidbody2D>();

            if (rigidbody != null)
            {
                rigidbody.velocity += (remainingDelta - newDelta) / Time.fixedDeltaTime;
            }

            remainingDelta = newDelta;
        }
Exemplo n.º 12
0
        protected virtual void Update()
        {
            // Get t value
            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            if (Clamp == true)
            {
                Angle = Mathf.Clamp(Angle, ClampMin, ClampMax);
            }

            // Lerp angle
            currentAngle = Mathf.LerpAngle(currentAngle, Angle, factor);

            // Update rotation
            transform.rotation = Quaternion.Euler(0.0f, 0.0f, -currentAngle);
        }
Exemplo n.º 13
0
        protected virtual void Update()
        {
            if (targetSet == true)
            {
                var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

                currentValue = Vector3.Lerp(currentValue, targetValue, factor);
                currentValue = Vector3.MoveTowards(currentValue, targetValue, Threshold * Time.deltaTime);

                Submit(currentValue);

                if (AutoStop == true && Vector3.SqrMagnitude(currentValue - targetValue) == 0.0f)
                {
                    Stop();
                }
            }
        }
Exemplo n.º 14
0
        protected override void UpdatePosition(float damping, float linear)
        {
            if (positionSet == true || Continuous == true)
            {
                if (destination != null)
                {
                    Position = destination.TransformPoint(DestinationOffset);
                }

                var currentPosition = transform.position;
                var targetPosition  = Position + Offset;

                if (IgnoreZ == true)
                {
                    targetPosition.z = currentPosition.z;
                }

                var direction = targetPosition - currentPosition;
                var velocity  = direction / Time.fixedDeltaTime;

                // Apply the velocity
                velocity *= LeanHelper.GetDampenFactor(damping, Time.fixedDeltaTime);
                velocity  = Vector3.MoveTowards(velocity, Vector3.zero, linear * Time.fixedDeltaTime);

                cachedRigidbody.velocity = velocity;
                Debug.Log(velocity);

                /*
                 * if (Rotation == true && direction != Vector3.zero)
                 * {
                 *      var angle           = Mathf.Atan2(direction.x, direction.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 *= LeanHelper.GetDampenFactor(RotationDamping, Time.fixedDeltaTime);
                 *
                 *      //cachedRigidbody.angularVelocity = angularVelocity;
                 * }
                 */
                fixedUpdateCalled = true;
            }
        }
Exemplo n.º 15
0
        protected virtual void LateUpdate()
        {
            if (Pivot != null)
            {
                var angles = Quaternion.LookRotation(Pivot.position - transform.position, Vector3.up).eulerAngles;

                Pitch = angles.x;
                Yaw   = angles.y;
            }

            // Get t value
            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            // Lerp the current values to the target ones
            currentPitch = Mathf.Lerp(currentPitch, Pitch, factor);
            currentYaw   = Mathf.Lerp(currentYaw, Yaw, factor);

            // Rotate to pitch and yaw values
            transform.localRotation = Quaternion.Euler(currentPitch, currentYaw, 0.0f);
        }
Exemplo n.º 16
0
        protected virtual void LateUpdate()
        {
            if (PitchClamp == true)
            {
                Pitch = Mathf.Clamp(Pitch, PitchMin, PitchMax);
            }

            if (YawClamp == true)
            {
                Yaw = Mathf.Clamp(Yaw, YawMin, YawMax);
            }

            // Get t value
            var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);

            // Lerp the current values to the target ones
            currentPitch = Mathf.Lerp(currentPitch, Pitch, factor);
            currentYaw   = Mathf.Lerp(currentYaw, Yaw, factor);

            // Rotate to pitch and yaw values
            transform.localRotation = Quaternion.Euler(currentPitch, currentYaw, 0.0f);
        }
Exemplo n.º 17
0
        protected virtual void Update()
        {
            var factor = LeanHelper.GetDampenFactor(damping, Time.deltaTime);

            UpdateRotation(factor);
        }
        protected virtual void LateUpdate()
        {
            // Make sure the camera exists
            var camera = LeanHelper.GetCamera(Camera, gameObject);

            if (camera != null)
            {
                if (Plane != null)
                {
                    var ray = Camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.0f));
                    var hit = default(Vector3);

                    if (Plane.TryRaycast(ray, ref hit, 0.0f, false) == true)
                    {
                        var oldPosition = transform.position;
                        var local       = Plane.transform.InverseTransformPoint(hit);
                        var snapped     = local;
                        var size        = new Vector2(Camera.orthographicSize * Camera.aspect, Camera.orthographicSize);

                        if (Plane.ClampX == true)
                        {
                            var min = Plane.MinX + size.x;
                            var max = Plane.MaxX - size.x;

                            if (min > max)
                            {
                                snapped.x = (min + max) * 0.5f;
                            }
                            else
                            {
                                snapped.x = Mathf.Clamp(local.x, min, max);
                            }
                        }

                        if (Plane.ClampY == true)
                        {
                            var min = Plane.MinY + size.y;
                            var max = Plane.MaxY - size.y;

                            if (min > max)
                            {
                                snapped.y = (min + max) * 0.5f;
                            }
                            else
                            {
                                snapped.y = Mathf.Clamp(local.y, min, max);
                            }
                        }

                        if (local != snapped)
                        {
                            var delta       = oldPosition - hit;
                            var newPosition = Plane.transform.TransformPoint(snapped) + delta;

                            if (Mathf.Approximately(oldPosition.x, newPosition.x) == false ||
                                Mathf.Approximately(oldPosition.y, newPosition.y) == false ||
                                Mathf.Approximately(oldPosition.z, newPosition.z) == false)
                            {
                                transform.position = newPosition;
                            }
                        }
                    }
                }
            }
            else
            {
                Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this);
            }
        }