コード例 #1
0
        private Vector2 CalculateRandomVector(ref ShakeInstance shake, ShakeSettings currentSettings)
        {
            float noise     = Mathf.PerlinNoise(Time.realtimeSinceStartup * m_DirectionNoiseScale, shake.duration);
            float deviation = noise * shake.magnitude * currentSettings.maxAngle;

            return(shake.direction.Rotate(deviation));
        }
コード例 #2
0
    private float CalculateShakeMagnitude(ref ShakeInstance shake, ShakeSettings currentSettings)
    {
        var t = shake.normalizedProgress;

        var noise = Mathf.PerlinNoise(Time.realtimeSinceStartup * m_MagnitudeNoiseScale, shake.duration);

        noise *= 0.6f + 0.4f;

        return(Mathf.Lerp(shake.magnitude, 0, t) * noise * currentSettings.maxShake);
    }
コード例 #3
0
        private float CalculateShakeMagnitude(ref ShakeInstance shake, ShakeSettings currentSettings)
        {
            float t = shake.normalizedProgress;

            float noise = Mathf.PerlinNoise(Time.realtimeSinceStartup * m_MagnitudeNoiseScale, shake.duration);

            // Rescale noise so it shakes primarily towards direction rather than in both directions
            // This changes the noise range from [1,-1] to [1, -0.2],
            noise *= 0.6f + 0.4f;

            return(Mathf.Lerp(shake.magnitude, 0, t) * noise * currentSettings.maxShake);
        }
コード例 #4
0
ファイル: ShakeButton.cs プロジェクト: Sogekeeper/ZODZ3
 public void UIToggleShake(bool enabled)
 {
     if (shakeInstance == null)
     {
         shakeInstance = Shaker.ShakeAll(shakePreset);
     }
     else
     {
         shakeInstance.Stop(shakePreset.FadeOut, true);
         shakeInstance = null;
     }
 }
コード例 #5
0
        /// <summary>
        /// Process and accumulate each shake
        /// </summary>
        protected virtual void ProcessShake(ref ShakeInstance shake, ref Vector2 shakeVector)
        {
            if (shake.maxDuration > 0)
            {
                shake.duration = Mathf.Clamp(shake.duration + Time.deltaTime, 0, shake.maxDuration);
            }

            ShakeSettings settings        = m_ShakingCamera.orthographic ? m_OrthographicSettings : m_PerspectiveSettings;
            float         magnitude       = CalculateShakeMagnitude(ref shake, settings);
            Vector2       additionalShake = CalculateRandomVector(ref shake, settings);

            shakeVector += additionalShake * magnitude;
        }
コード例 #6
0
        /// <summary>
        /// Stop a perpetual screenshake
        /// </summary>
        public bool StopShake(int shakeId)
        {
            // Find shake
            for (int i = m_CurrentShakes.Count - 1; i >= 0; --i)
            {
                ShakeInstance shake = m_CurrentShakes[i];

                if (shake.shakeId == shakeId)
                {
                    shake.StopShake();
                    m_CurrentShakes[i] = shake;
                    return(true);
                }
            }

            return(false);
        }
コード例 #7
0
        /// <summary>
        /// Do shakes
        /// </summary>
        protected virtual void Update()
        {
            // Double check that our camera still exists
            if (m_ShakingCamera == null)
            {
                return;
            }

            Vector2 shakeIntensity = Vector2.zero;

            // Count backwards so we can remove shakes with simpler logic
            for (int i = m_CurrentShakes.Count - 1; i >= 0; --i)
            {
                ShakeInstance shake = m_CurrentShakes[i];

                ProcessShake(ref shake, ref shakeIntensity);

                if (shake.done)
                {
                    m_CurrentShakes.RemoveAt(i);
                }
                else
                {
                    // Update list
                    m_CurrentShakes[i] = shake;
                }
            }

            Vector3 shake3D = new Vector3(shakeIntensity.x, shakeIntensity.y, 0);

            if (m_ShakingCamera.orthographic)
            {
                // Orthographic cameras get translated
                transform.localPosition = shake3D;
                transform.localRotation = Quaternion.identity;
            }
            else
            {
                // Perspective cameras get a shake
                Vector3 rotateAxis = Vector3.Cross(Vector3.forward, shake3D).normalized;

                transform.localPosition = Vector3.zero;
                transform.localRotation = Quaternion.AngleAxis(shake3D.magnitude, rotateAxis);
            }
        }
コード例 #8
0
    public void DoShake(Vector2 direction, float magnitude, float duration)
    {
        // Add a new shake
        var shake = new ShakeInstance
        {
            maxDuration = duration,
            duration    = 0,
            magnitude   = magnitude,
            direction   = direction
        };

        m_CurrentShakes.Add(shake);

        if (m_ShakeCounter == int.MaxValue)
        {
            m_ShakeCounter = 0;
        }
    }
コード例 #9
0
        /// <summary>
        /// Enable a repeating screen shake
        /// </summary>
        /// <param name="direction">The direction in screen space of the shake. Should be normalized</param>
        /// <param name="magnitude">The magnitude of the shake (0-1)</param>
        /// <returns>Index of shake so it can be stopped later</returns>
        public int DoPerpetualShake(Vector2 direction, float magnitude)
        {
            int result = m_ShakeCounter;

            // Add a new shake
            ShakeInstance shake = new ShakeInstance
            {
                shakeId     = m_ShakeCounter++,
                maxDuration = -1,
                duration    = 0,
                magnitude   = magnitude,
                direction   = direction
            };

            m_CurrentShakes.Add(shake);

            if (m_ShakeCounter == int.MaxValue)
            {
                m_ShakeCounter = 0;
            }

            return(result);
        }
コード例 #10
0
    void Update()
    {
        // Follow
        transform.position = Vector3.Lerp(transform.position, followObject.position, maxFollowSpeed * Time.deltaTime);

        // Shake
        float currentShakeIntensity = 0;

        for (int i = 0; i < shakeInstances.Count; i++)
        {
            ShakeInstance currentShakeInstance = shakeInstances[i];
            if (currentShakeInstance.Duration <= 0)
            {
                shakeInstances.RemoveAt(i);
            }
            else
            {
                currentShakeIntensity         = Mathf.Max(currentShakeIntensity, currentShakeInstance.Intensity * currentShakeInstance.Duration / currentShakeInstance.FullDuration);
                currentShakeInstance.Duration = currentShakeInstance.Duration - Time.unscaledDeltaTime;
            }
        }

        if (shakeInstances.Count > 0)
        {
            cameraGroupTransform.localPosition = initialCameraPosition + Random.insideUnitSphere * currentShakeIntensity;
        }
        else
        {
            cameraGroupTransform.localPosition = initialCameraPosition;
        }

        /*
         * // FoV
         * cameraComponent.fieldOfView = Mathf.Lerp(cameraComponent.fieldOfView, targetCameraFOV, fovRateOfChange * Time.deltaTime);
         */
    }
コード例 #11
0
    public void ShakeCamera(float intensity, float duration)
    {
        ShakeInstance newShakeInstance = new ShakeInstance(intensity, duration);

        shakeInstances.Add(newShakeInstance);
    }
コード例 #12
0
 void Start()
 {
     camShakeInst = camShaker.Shake(shakeFear);
     camShakeInst.Stop(0f, false);
 }
コード例 #13
0
    protected void Update()
    {
        float currentShakeIntensity = 0;

        for (int i = 0; i < shakeInstances.Count; i++)
        {
            ShakeInstance currentShakeInstance = shakeInstances [i];

            if (currentShakeInstance.currentShakeDuration <= 0)
            {
                shakeInstances.RemoveAt(i);
            }
            else
            {
                float currentShakeInstanceIntensity = currentShakeInstance.shakeIntensity * currentShakeInstance.currentShakeDuration / currentShakeInstance.shakeDuration;
                currentShakeIntensity = Mathf.Max(currentShakeInstanceIntensity, currentShakeIntensity);
                float currentShakeInstanceDuration = currentShakeInstance.currentShakeDuration;
                currentShakeInstance.currentShakeDuration = currentShakeInstanceDuration - Time.deltaTime;
            }
        }
        cameraTransform.localPosition = cameraInitialLocalPosition + Random.insideUnitSphere * currentShakeIntensity;

        float   minY            = Mathf.Infinity;
        float   maxY            = Mathf.NegativeInfinity;
        float   minX            = Mathf.Infinity;
        float   maxX            = Mathf.NegativeInfinity;
        Vector3 averagePosition = Vector2.zero;

        for (int i = 0; i < trackedTransforms.Count; i++)
        {
            float currentTrackedTransformPositionX = trackedTransforms[i].position.x;
            float currentTrackedTransformPositionY = trackedTransforms[i].position.y;
            if (currentTrackedTransformPositionX > maxX)
            {
                maxX = currentTrackedTransformPositionX;
            }
            if (currentTrackedTransformPositionX < minX)
            {
                minX = currentTrackedTransformPositionX;
            }
            if (currentTrackedTransformPositionY > maxY)
            {
                maxY = currentTrackedTransformPositionY;
            }
            if (currentTrackedTransformPositionY < minY)
            {
                minY = currentTrackedTransformPositionY;
            }
            averagePosition += trackedTransforms[i].position;
        }
        averagePosition /= trackedTransforms.Count;
        //Debug.Log(minX + " " + maxX + " " + minY + " " + maxY);
        float charactersHeightDifference = maxY - minY;
        float charactersWidthDifference  = maxX - minX;
        //Debug.Log(charactersHeightDifference + " " + charactersWidthDifference);
        float idealHeight = charactersHeightDifference + 2 * cameraBuffer;
        float idealWidth  = charactersWidthDifference + 2 * cameraBuffer;
        float calculatedHeightUsingIdealWidth = idealWidth / mainCamera.aspect;

        //Debug.Log(idealHeight + " " + calculatedHeightUsingIdealWidth);
        idealHeight = Mathf.Max(idealHeight, calculatedHeightUsingIdealWidth);
        float distanceFromAveragePositionToCameraPositionOnZAxis = Mathf.Abs(averagePosition.z - transform.position.z);
        float idealFieldOfView = 2 * Mathf.Atan2(idealHeight, 2 * distanceFromAveragePositionToCameraPositionOnZAxis) * Mathf.Rad2Deg;

        averagePosition.z  = cameraZValue;
        averagePosition   += (Vector3)cameraOffset;
        transform.position = Vector3.Lerp(transform.position, averagePosition, cameraScaleSpeed * Time.deltaTime);

        mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, idealFieldOfView, cameraScaleSpeed * Time.deltaTime);
    }