//constant spring force. void UpdateEarthQuake() { if (m_client == null || !m_client.UseEarthQuake || m_earthQuakeTimeTemp <= 0.0f || !EarthQuakeToggled) { return; } m_elapsedTime += Time.deltaTime; m_earthQuakeTimeTemp -= 0.0166f * (60 * Time.deltaTime); float magnitude = 0f; if (m_client.EarthQuakeMagTye == MAGTYPE.Fixed) { magnitude = m_client.EarthQuakeMagnitude; } else { magnitude = m_client.EarthQuakeMagCurve.Evaluate(m_elapsedTime); } // the horisontal move is a perlin noise value between 0 and // 'EarthQuakeMagnitude' (depending on 'EarthQuakeTime'). // horizMove will ease out during the last second. Vector3 horizMove = Vector3.Scale(SmoothRandom.GetVector3Centered(1), new Vector3(magnitude, 0, magnitude)) * Mathf.Min(m_earthQuakeTimeTemp, 1.0f); // the vertical move is half the earthquake magnitude and // has a 30% chance of occuring each frame. when it does, // it alternates between positive and negative. this produces // sharp shakes with nice spring smoothness inbetween. // vertMove will ease out during the last second. float vertMove = 0; if (UnityEngine.Random.value < 0.3f) { vertMove = UnityEngine.Random.Range(0, (magnitude * 0.35f)) * Mathf.Min(m_earthQuakeTimeTemp, 1.0f); if (PositionSpring.State.y >= PositionSpring.RestState.y) { vertMove = -vertMove; } } PositionSpring.AddForce(horizMove); // apply earthquake roll force on the camera rotation spring RotationSpring.AddForce(new Vector3(0, 0, -horizMove.x * 2) * m_client.EarthQuakeCameraRollFactor); // vertical move is always applied to the camera spring. PositionSpring.AddForce(new Vector3(0, vertMove, 0)); }
/// <summary> /// Updates the effect. /// </summary> public override void Update() { // Stop the effect if it has occurred for more than the duration. var endTime = m_StartTime + m_Duration; if (endTime < Time.unscaledTime) { StopEffect(); return; } var force = Vector3.zero; if (m_SmoothHorizontalForce) { // Apply a horizontal force which is the perlin noise value between 0 and the force. This force will ease out during the specified fade out duration. force.x = SmoothRandom.GetVector3Centered(1).x *m_Force.x *Mathf.Min(endTime - Time.unscaledTime, m_FadeOutDuration) * Time.timeScale * m_CharacterLocomotion.TimeScale; } else { // If smooth horizontal force is false then apply a random force which will ease out during the specified fade out duration. force.x = Random.Range(-m_Force.x, m_Force.x) * Mathf.Min(endTime - Time.unscaledTime, m_FadeOutDuration); // Alternates between positive and negative to produce sharp shakes with nice spring smoothness. if (Mathf.Sign(m_TotalForce.x) == Mathf.Sign(force.x)) { force.x = -force.x; } } // Restrict the number of times a vertical force is applied to prevent a jerky movements. if (Random.value <= m_VerticalForceProbability) { // Smoothly fade out during the specified fade out duration. force.y = Random.Range(0, m_Force.y) * Mathf.Min(endTime - Time.unscaledTime, m_FadeOutDuration); // Alternates between positive and negative to produce sharp shakes with nice spring smoothness. if (Mathf.Sign(m_TotalForce.y) == Mathf.Sign(force.y)) { force.y = -force.y; } } m_TotalForce += force; // Add the force to the camera. if ((m_Target & ShakeTarget.Camera) != 0 && m_CameraController != null) { m_CameraController.AddPositionalForce(force * m_PositionalFactor); m_CameraController.AddRotationalForce(-force * 2 * m_RotationalFactor); } // Add the force to the item. if ((m_Target & ShakeTarget.Item) != 0) { var positionalForce = Vector3.forward * force.x * 0.015f; var rotationalForce = positionalForce; rotationalForce.Set(force.y * 2, -force.x, force.x * 2); EventHandler.ExecuteEvent(m_GameObject, "OnAddSecondaryForce", -1, positionalForce, rotationalForce, true); } // Add the horizontal force to the character. if ((m_Target & ShakeTarget.Character) != 0) { force.y = 0; m_CharacterLocomotion.AddForce(force * m_PositionalFactor); } }