Use Accelerometer for Roll-a-ball Movement

I’d like to use the accelerometer to control the roll-a-ball from this awesome tutorial on my iPhone and am having some trouble finding out how to do so. I’ve been able to get it to work along one axis (x) somewhat properly, but when I enable the y axis the ball jumps up in the air rather than rolls north / south (phone is held in landscape mode with home button on right).

I’ve tried adding a z axis to the script but it didn’t seem to do anything at all. Can you please assist? I’m using C# and here’s the full script below:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
	public float speed;
	public float force = 11.0f; //used for accelerometer controls

	void FixedUpdate ()
	{
	
	//are we using a computer as opposed to a mobile device with an accelerometer?
	if(SystemInfo.deviceType == DeviceType.Desktop)
		{
		//start of movement code from rollerball tutorial
		//used for desktop movement
		float moveHorizontal = Input.GetAxis("Horizontal");
		float moveVertical = Input.GetAxis("Vertical");
		
		Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
		
		rigidbody.AddForce(movement * speed * Time.deltaTime);
		//end of movement code from rollerball tutorial
		}
		//or are we on mobile with an accelerometer?
		else
		{
			//start accelerometer code here
			Vector3 dir = Vector3.zero;
			dir.x = Input.acceleration.x;
			//dir.y = Input.acceleration.y;
			Physics.gravity = dir * force;
		}
	
	}

	void OnTriggerEnter(Collider other) 
	{
		if(other.gameObject.tag == "Pickup")
		{
			other.gameObject.SetActive(false);
		}
	}
}

I have been looking the same and I found a code that works, replace it for your code on the accelerometer code here section. speedAc is a float that I give a value of 10 and works fine.

curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
GetAxisV = Mathf.Clamp(curAc.y * sensV, -1, 1);
GetAxisH = Mathf.Clamp(curAc.x * sensH, -1, 1);
// now use GetAxisV and GetAxisH instead of Input.GetAxis vertical and horizontal
// If the horizontal and vertical directions are swapped, swap curAc.y and curAc.x
// in the above equations. If some axis is going in the wrong direction, invert the
// signal (use -curAc.x or -curAc.y)
			
Vector3 movement = new Vector3 (GetAxisH, 0.0f, GetAxisV);
rigidbody.AddForce(movement * speedAc);

Even I would appreciate some tutorial/examples regarding different inputs from mobile devices.

This one works for me:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    // Using same speed reference in both, desktop and other devices
	public float speed =1000;

	void Main ()
		{
                // Preventing mobile devices going in to sleep mode 
                //(actual problem if only accelerometer input is used)
				Screen.sleepTimeout = SleepTimeout.NeverSleep;
		}

	void Update()
		{
         
		if (SystemInfo.deviceType == DeviceType.Desktop) 
		{
            // Exit condition for Desktop devices
			if (Input.GetKey("escape"))
				Application.Quit();
		}
		else
		{
            // Exit condition for mobile devices
			if (Input.GetKeyDown(KeyCode.Escape))
				Application.Quit();			
		}
		
		

		}
	
	
	void FixedUpdate ()
		{
			if (SystemInfo.deviceType == DeviceType.Desktop) 
			{ 
                    // Player movement in desktop devices

                // Definition of force vector X and Y components
				float moveHorizontal = Input.GetAxis("Horizontal");
				float moveVertical = Input.GetAxis("Vertical");
				// Building of force vector
				Vector3 movement = new Vector3 (moveHorizontal,0.0f,moveVertical);
				// Adding force to rigidbody
				rigidbody.AddForce(movement * speed * Time.deltaTime);
			}
			else
			{
                    // Player movement in mobile devices

                // Building of force vector 
				Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, Input.acceleration.y);
                // Adding force to rigidbody
				rigidbody.AddForce(movement * speed * Time.deltaTime);
			}


		}

		
}

This guy is explaining it for you:


And have you looked a this video?
https://unity3d.com/learn/tutorials/modules/beginner/platform-specific/accelerometer-input

Or this code?

Sure, but I’m actually wondering what zeroAc is. Because I don’t subtract Input.acceleration by it in my version and it causes the accelerometer to be way too sensitive.

sing UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {
public float speed,force;

void FixedUpdate(){
	if (SystemInfo.deviceType == DeviceType.Desktop) {
					float moveHorizontal = Input.GetAxis ("Horizontal");
					float moveVertical = Input.GetAxis ("Vertical");
					Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
					rigidbody.AddForce (movement * speed * Time.deltaTime);
			} else {
		float moveHorizontal = Input.acceleration.x;
		float moveVertical = Input.acceleration.y;
		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
		rigidbody.AddForce (movement * speed * Time.deltaTime);
			}
	}

}

void FixedUpdate () {
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);

		Vector3 acceleration = Input.acceleration;

		Vector3 movement = (
			moveHorizontal != 0.0f || moveVertical != 0.0f ? 
			new Vector3 (moveHorizontal, 0.0f, moveVertical) :
			new Vector3 (acceleration.x, 0.0f, acceleration.y)
		);

		GetComponent<Rigidbody> ().AddForce (movement * speed);
	}

I have some rollaball code which uses gyro and joystick. I am not at my work pc but will post fully working script in the morning (C#).

What! is the Best
float speed = ???;
for roll a ball good control