public static ShakeResult operator +(ShakeResult a, ShakeResult b)
        {
            ShakeResult c = new ShakeResult();

            c.PositionShake = a.PositionShake + b.PositionShake;
            c.RotationShake = a.RotationShake + b.RotationShake;

            return(c);
        }
        private void Update()
        {
            ShakeResult shake = new ShakeResult();

            for (int i = 0; i < activeShakes.Count; i++)
            {
                if (activeShakes[i].IsFinished)
                {
                    activeShakes.RemoveAt(i);
                    i--;
                    continue;
                }

                shake += activeShakes[i].UpdateShake(Time.deltaTime);
            }

            transform.localPosition    = shake.PositionShake;
            transform.localEulerAngles = shake.RotationShake;
        }
Esempio n. 3
0
        /// <summary>
        /// Updates the shake timers and returns the resulting shake values.
        /// </summary>
        /// <param name="deltaTime">The delta time value to use. Typically this will be Time.deltaTime.</param>
        /// <returns>A ShakeResult containing the position and rotation shake amounts.</returns>
        public ShakeResult UpdateShake(float deltaTime)
        {
            ShakeResult result = new ShakeResult();

            //Get shake values
            result.PositionShake = getPositionShake();
            result.RotationShake = getRotationShake();

            //Update timers
            //Protection for updating timers more than once per frame.
            if (Time.frameCount == lastUpdatedFrame)
            {
                return(result);
            }

            //Update pause timer
            if (pauseFadeTime > 0)
            {
                if (IsPaused)
                {
                    pauseTimer += deltaTime / pauseFadeTime;
                }
                else
                {
                    pauseTimer -= deltaTime / pauseFadeTime;
                }
            }
            pauseTimer = Mathf.Clamp01(pauseTimer);

            //Update noise timer
            noiseTimer += (1 - pauseTimer) * deltaTime * CurrentRoughness;

            //Update fade timer
            if (State == ShakeState.FadingIn)
            {
                if (fadeInTime > 0)
                {
                    fadeTimer += deltaTime / fadeInTime;
                }
                else
                {
                    fadeTimer = 1;
                }
            }
            else if (State == ShakeState.FadingOut)
            {
                if (fadeOutTime > 0)
                {
                    fadeTimer -= deltaTime / fadeOutTime;
                }
                else
                {
                    fadeTimer = 0;
                }
            }
            fadeTimer = Mathf.Clamp01(fadeTimer);

            //Update the state if needed
            if (fadeTimer == 1)
            {
                if (ShakeParameters.ShakeType == ShakeType.Sustained)
                {
                    State = ShakeState.Sustained;
                }
                else if (ShakeParameters.ShakeType == ShakeType.OneShot)
                {
                    Stop(ShakeParameters.FadeOut, true);
                }
            }
            else if (fadeTimer == 0)
            {
                State = ShakeState.Stopped;
            }

            lastUpdatedFrame = Time.frameCount;

            return(result);
        }