How can I detect a shake motion on a mobile device using Unity3D? C# -


i have assumed unity has event trigger can't find 1 in unity3d documentation. need work changes in accelerometer?

thank all.

excellent discussion regarding detecting "shaking" can found in this thread on unity forums.

from brady's post:

from can tell in of apple iphone sample apps, set vector magnitude threshold, set high-pass filter on accelerometer values, if magnitude of acceleration vector ever longer set threshold, it's considered "shake".

jmpp's suggested code (modified readability , closer valid c#):

float accelerometerupdateinterval = 1.0f / 60.0f; // greater value of lowpasskernelwidthinseconds, slower // filtered value converge towards current input sample (and vice versa). float lowpasskernelwidthinseconds = 1.0f; // next parameter initialized 2.0 per apple's recommendation, // or @ least according brady! ;) float shakedetectionthreshold = 2.0f;  float lowpassfilterfactor; vector3 lowpassvalue;  void start() {     lowpassfilterfactor = accelerometerupdateinterval / lowpasskernelwidthinseconds;     shakedetectionthreshold *= shakedetectionthreshold;     lowpassvalue = input.acceleration; }  void update() {     vector3 acceleration = input.acceleration;     lowpassvalue = vector3.lerp(lowpassvalue, acceleration, lowpassfilterfactor);     vector3 deltaacceleration = acceleration - lowpassvalue;      if (deltaacceleration.sqrmagnitude >= shakedetectionthreshold)     {         // perform "shaking actions" here. if necessary, add suitable         // guards in if check above avoid redundant handling during         // same shake (e.g. minimum refractory period).         debug.log("shake event detected @ time "+time.time);     } } 

note: recommend read whole thread full context.


Comments