Exemplo n.º 1
0
        private void LateUpdate()
        {
            focusBox.Update(target.GetComponent <Collider2D>().bounds);
            var focalPoint = focusBox.center + Vector2.up * cameraHeight;

            Vector2 inputVector = Vector2.zero;

            if (targetInputProvider != null)
            {
                inputVector = targetInputProvider.GetInputVector();
            }

            // Handle lookahead - both player input and focus box must be moving in the same direction.
            if (focusBox.velocity.x != 0)
            {
                lookAheadDirection = Mathf.Sign(focusBox.velocity.x);
                var idealLookAheadX = lookAheadDirection * lookAheadDistance;
                if (Mathf.Sign(inputVector.x) == Mathf.Sign(focusBox.velocity.x) &&
                    inputVector.x != 0)
                {
                    isLookingAhead   = true;
                    targetLookAheadX = idealLookAheadX;
                }
                else if (isLookingAhead)
                {
                    isLookingAhead   = false;
                    targetLookAheadX = currentLookAheadX + (idealLookAheadX - currentLookAheadX) / 4f;
                }
            }

            // Apply smoothing.
            currentLookAheadX = Mathf.SmoothDamp(
                currentLookAheadX, targetLookAheadX, ref smoothVelocityX, smoothTime);
            focalPoint.y = Mathf.SmoothDamp(
                transform.position.y, focalPoint.y, ref smoothVelocityY, smoothTime);
            focalPoint += Vector2.right * currentLookAheadX;

            // Set final camera position.
            var finalPosition = (Vector3)focalPoint + Vector3.forward * -10f;

            transform.position = new Vector3(
                lockXAxis ? initialPosition.x : finalPosition.x,
                lockYAxis ? initialPosition.y : finalPosition.y,
                finalPosition.z);
        }