Rigidbody with Accelerometer

Hello community! I wanted to control a rigidbody by using accelerometer on android, i printed something like this:

function Update() {
if((Input.acceleration.x) > 0)
    {
        rigidbody.AddForce(Input.acceleration.x, 0, 0);
    }
else if((Input.acceleration.x) < 0)
    {
        rigidbody.AddForce(Input.acceleration.x, 0, 0);
    }
else
    {
        rigidbody.AddForce(Input.acceleration.x, 0, 0);
    }
}

But nothing worked. So, i hope that Input.accelereation.x is integer, since i can’t find any information about this function ^^.

Hope for a fast answer =) Thx

Look, imagine that rigidbody located at a big hill, then it will roll down, and while it is, you can move rigidbody with accelerometer, to the left and right.

I finally fixed it. Just had to use RigidbodyConstraints to freeze the rotation of the rigidbody. It was rotating on different axes, which was causing it to move in the wrong direction.

Just try this in a script and see what it gives you.

var dir : Vector3 = Vector3.zero;
	
	function Update ()
	{
		dir.x = -Input.acceleration.x;
		dir.y = Input.acceleration.y;
		dir.z = Input.acceleration.z;
	}
	
	function OnGUI()
	{
		GUI.Box (Rect (20,20,200,200),dir);
	}

You will find that x,y,z vary between -1 → 0 → 1 depending on the way the device is held.
And, it’s a Float