How to calibrate accelerometer according to start ?

I think i have a “should be” working code but i don’t know why it is not working. With this code too i have to hold the phone straight to get it to work. My present code is below.
Any help is appreciated :slight_smile:

#pragma strict
var speed : float;
var accelerationX : float = 0;
var accelerationZ : float = 0;
var zatstart : float = 0;
function Start () {
	zatstart = Input.acceleration.z;
}

function Update () {
	var dir : float;
	dir = (Input.acceleration.z-zatstart);
	if (dir > 1){
	    dir = 1;
	}
	accelerationX = Input.acceleration.x;
	accelerationZ = -1*(dir);
	GetComponent.<Rigidbody>().AddRelativeForce(new Vector3(accelerationX * speed,0,accelerationZ * speed));
}

I had modified the Roll-a-ball tutorial by using my smartphone’s accelerometer.This is what I had done

	public float speed = 6;
    private RigidBody rb;
	private float zatstart = 0;
	private float xatstart = 0;
	private float hzMovement = 0, vtMovement = 0;
	public Button calibrate;
    public Toggle accel;

    void Start(){
                calib();
                rb = GetComponent<Rigidbody> ();
   }
    void Update(){
                calibrate.onClick.AddListener (calib);
    			if (accel.isOn) {
    				hzMovement = Input.acceleration.x - xatstart;
    				vtMovement = -Input.acceleration.z + zatstart;
    			} else {
    				hzMovement = CrossPlatformInputManager.GetAxis ("Horizontal");
    				vtMovement = CrossPlatformInputManager.GetAxis ("Vertical");
    			}
    		Vector3 v = new Vector3 (hzMovement, 0, vtMovement);
    		rb.AddForce (v * speed);
    	}

    void calib(){
    		xatstart = Input.acceleration.x;
    		zatstart = Input.acceleration.z;
    	}

The code will be pretty much same in both javascript and c#.Hope this helps.