public void onShake(float shakeProgress) { if (OnShake != null) { OnShake.Invoke(shakeProgress); } }
private IEnumerator ScreenShakeCoroutine(float intensity) { if (intensity > _totalShakeIntensity) { _totalShakeIntensity = intensity; } // TODO: Improvement // Would prefer to use tweens here, but need to think of a way // to prevent smaller shakes from stopping bigger shakes, or // smaller shakes preventing bigger shakes. while (_totalShakeIntensity > 0) { var time = Time.timeSinceLevelLoad * shakeSpeed; float x = (Mathf.PerlinNoise(1, time) - 0.5f) * 2f; float y = (Mathf.PerlinNoise(10, time) - 0.5f) * 2f; float z = (Mathf.PerlinNoise(100, time) - 0.5f) * 2f; var shakeOffset = new Vector3(x, y, z) * _totalShakeIntensity; OnShake?.Invoke(shakeOffset); _totalShakeIntensity -= shakeDecay * Time.deltaTime; yield return(null); } }
void Update() { Quaternion oldHeadRotation = _headRotation; _headRotation = _headTransform.localRotation; float angleRotated = Quaternion.Angle(_headRotation, oldHeadRotation); if (angleRotated > _rotationPerSecondShakeThreshold * Time.deltaTime) { if (!_wasShakeFiredInCurrentDirection) { _wasShakeFiredInCurrentDirection = true; OnShake?.Invoke(); } } else { _wasShakeFiredInCurrentDirection = false; } }
public void OnSensorChanged(SensorEvent e) { const int EventTimeLimit = 100; var current = System.Environment.TickCount; var updateDelta = current - lastUpdate; if (updateDelta < EventTimeLimit) { return; } const int ShakeThreshold = 350; var x = e.Values[0]; var y = e.Values[1]; var z = e.Values[2]; var delta = x + y + z - lastX - lastY - lastZ; var speed = Math.Abs(delta) / updateDelta * 1000; const int ShakesRequired = 3; if (speed > ShakeThreshold) { lastShake = current; _count++; if (_count >= ShakesRequired) { //shake happened _count = 0; OnShake?.Invoke(this, new EventArgs()); } } lastUpdate = current; lastX = x; lastY = y; lastZ = z; }