how to shake phone using c#?

I have write this script:

if(Mathf.Abs(Input.acceleration.y) > 1.5f){

		Debug.Log("Y =  "  + Input.acceleration.y);
		
	}

And i want to know when it shake up or shake down.Because it’s value always return negative when i shake up or shake down.

It depends on in which direction you’re shaking your device. Take a look at the picture below, where the accelerometer axes are shown for a typical device:

alt text

In this position, Y points roughly in the vertical direction while X is horizontal. The values returned by the accelerometer in this case are something like X = 0, Y = -0.7, Z = -0.7.

Now let’s understand how the accelerometer works: internally, it’s a mass suspended by springs aligned with the X, Y and Z directions, and return values proportional to the deformation of each spring (take a look at this article for more info). When you move the device up, the internal mass “lags down” due to its inertia, thus the Y value becomes more negative; when you move it down, the inertia makes the mass “lag up” for a while, and Y becomes less negative. The result is that shaking the device actually produces peaks above and below its average value. In order to detect these peaks, subtract the average accelerometer value from the current reading and compare it to some threshold: if it’s above the limit, you’ve got a peak. If you want to count how many times the device was shaken, count only the peaks of a given polarity (only positive or only negative), and finish the counting when you have no peaks during about 0.6 seconds (typical shaking produces peaks each 0.2 to 0.3 seconds). The whole thing can be implemented in a simple function that should be called every frame:

using UnityEngine;
using System.Collections;

public class MeasureShaking: MonoBehaviour {
  public float avrgTime = 0.5f;
  public float peakLevel = 0.6f;
  public float endCountTime = 0.6f;
  public int shakeDir;
  public int shakeCount;
   
  Vector3 avrgAcc = Vector3.zero;
  int countPos;
  int countNeg;
  int lastPeak;
  int firstPeak;
  bool counting;
  float timer;
   
  bool ShakeDetector(){
    // read acceleration:
    Vector3 curAcc = Input.acceleration; 
    // update average value:
    avrgAcc = Vector3.Lerp(avrgAcc, curAcc, avrgTime * Time.deltaTime);
    // calculate peak size:
    curAcc -= avrgAcc;
    // variable peak is zero when no peak detected...
    int peak = 0;
    // or +/- 1 according to the peak polarity:
    if (curAcc.y > peakLevel) peak = 1;
    if (curAcc.y < -peakLevel) peak = -1;
    // do nothing if peak is the same of previous frame:
    if (peak == lastPeak) 
      return false;
    // peak changed state: process it
    lastPeak = peak; // update lastPeak
    if (peak != 0){ // if a peak was detected...
      timer = 0; // clear end count timer...
      if (peak > 0) // and increment corresponding count
        countPos++;
      else
        countNeg++;
      if (!counting){ // if it's the first peak...
        counting = true; // start shake counting
        firstPeak = peak; // save the first peak direction
      }
    } 
    else // but if no peak detected...
    if (counting){ // and it was counting...
      timer += Time.deltaTime; // increment timer
      if (timer > endCountTime){ // if endCountTime reached...
        counting = false; // finish counting...
        shakeDir = firstPeak; // inform direction of first shake...
        if (countPos > countNeg) // and return the higher count
          shakeCount = countPos;
        else
          shakeCount = countNeg;
        // zero counters and become ready for next shake count
        countPos = 0;
        countNeg = 0;
        return true; // count finished
      }
    }
    return false;
  }
   
  function Update(){
    if (ShakeDetector()){ // call ShakeDetector every Update!
      // the device was shaken up and the count is in shakeCount
      // the direction of the first shake is in shakeDir (1 or -1)
    }
    // the variable counting tells when the device is being shaken:
    if (counting){
      print("Shaking up device");
    }
  }
}

JS version:

var avrgTime: float = 0.5;
var peakLevel: float = 0.6;
var endCountTime: float = 0.6;
var shakeDir: int;
var shakeCount: int;

private var avrgAcc: Vector3 = Vector3.zero;
private var countPos: int;
private var countNeg: int;
private var lastPeak: int;
private var firstPeak: int;
private var counting: boolean;
private var timer: float;

function ShakeDetector(): boolean {
  // read acceleration:
  var curAcc: Vector3 = Input.acceleration; 
  // update average value:
  avrgAcc = Vector3.Lerp(avrgAcc, curAcc, avrgTime * Time.deltaTime);
  // calculate peak size:
  curAcc -= avrgAcc;
  // variable peak is zero when no peak detected...
  var peak = 0;
  // or +/- 1 according to the peak polarity:
  if (curAcc.y > peakLevel) peak = 1;
  if (curAcc.y < -peakLevel) peak = -1;
  // do nothing if peak is the same of previous frame:
  if (peak == lastPeak) 
    return false;
  // peak changed state: process it
  lastPeak = peak; // update lastPeak
  if (peak != 0){ // if a peak was detected...
    timer = 0; // clear end count timer...
    if (peak > 0) // and increment corresponding count
      countPos++;
    else
      countNeg++;
    if (!counting){ // if it's the first peak...
      counting = true; // start shake counting
      firstPeak = peak; // save the first peak direction
    }
  } 
  else // but if no peak detected...
  if (counting){ // and it was counting...
    timer += Time.deltaTime; // increment timer
    if (timer > endCountTime){ // if endCountTime reached...
      counting = false; // finish counting...
      shakeDir = firstPeak; // inform direction of first shake...
      if (countPos > countNeg) // and return the higher count
        shakeCount = countPos;
      else
        shakeCount = countNeg;
      // zero counters and become ready for next shake count
      countPos = 0;
      countNeg = 0;
      return true; // count finished
    }
  }
  return false;
}

function Update(){
  if (ShakeDetector()){ // call ShakeDetector every Update!
    // the device was shaken up and the count is in shakeCount
    // the direction of the first shake is in shakeDir (1 or -1)
  }
  // the variable counting tells when the device is being shaken:
  if (counting){
    print("Shaking up device");
  }
}