Exemple #1
0
        /// <summary>
        /// Normalizes the given dimension and position to its normalized value
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="position"></param>
        /// <returns>The normalized value(-1 to 1)</returns>
        private float Normalized(float dimension, float position)
        {
            float val = Geometryf.NormalizeValue(0, dimension, position);

            val = Geometryf.MaxValue(1, val);

            val = System.Math.Abs(val) < cameraLerpThreshold ? 0 : val;
            return(val);
        }
Exemple #2
0
        public void Update()
        {
            // Get the current player ship
            Ship toFollow = PlayerController.Instance.Ship;

            if (toFollow != null)
            {
                float shipX = toFollow.transform.position.x;
                float shipY = toFollow.transform.position.y;

                // Calculate the acceleration delta
                Vector2 accelerationDelta = new Vector2(
                    Geometryf.MaxValue(maxAccelerationDistance, acceleration.x / 100),
                    Geometryf.MaxValue(maxAccelerationDistance, acceleration.y / 100));

                // Calculate the new camera x, y position
                Vector2 normalizedVector = GetCenterMouseOffset(
                    new Vector2(Screen.width, Screen.height),
                    Input.mousePosition);

                targetPosition = new Vector2(
                    cameraLerp * (normalizedVector.x + accelerationDelta.x),
                    cameraLerp * (normalizedVector.y + accelerationDelta.y)
                    );

                currentPosition = Vector2.Lerp(currentPosition, targetPosition, Time.deltaTime * 3);

                // Calculate the new camera zoom (z axis)
                targetZoom += Input.mouseScrollDelta.y;
                targetZoom  = targetZoom < 0 ? targetZoom : 0;
                targetZoom  = targetZoom > -maxCameraZ ? targetZoom : -maxCameraZ;
                currentZoom = Mathf.Lerp(currentZoom, targetZoom, Time.deltaTime * 2);

                // Set the new position
                transform.position = new Vector3(shipX + currentPosition.x, shipY + currentPosition.y, -10 + currentZoom);
            }
        }