Device Shake Detection C# for Unity

Here is a fast and great way to detect Shaking on a device with motion detection.

//set what your threshold to detect shaking should be
var AccelerationThreshold = 2.0;
 
function Update () {
 
var accel = iPhoneInput.acceleration;
 
   var x = accel.x;
   var y = accel.y;
   var z = accel.z;
   
  var length = Mathf.Sqrt(x * x + y * y + z * z);
   
      if (length >= AccelerationThreshold ) {
 
        // setup something to happen here
       /*
       REPLACE ME WITH CODE
       */
        }
}  

Done! Just write the code that you want to trigger on shake! :handshake:

1 Like