C# Move object by tilting on android device.

I got a platform with a Spher on it in a 3D space. What I want to do, is to make the sphere move freely up and down the X-Axis and the Z -Axis by tilting the phone. So when I tilt the phone to the righ the sphere rolls to the right, and when I tilt it to the left it rolls to the left and so on.

I can easily write a script that works with input from a keyboard on a PC, it would look something like this:

float amountToMove = movementSpeed * Time.deltaTime;
Vector3 movement = (Input.GetAxis("Horizontal") * -Vector3.left * amountToMove) + (Input.GetAxis("Vertical") * Vector3.forward * amountToMove);
rigidbody.AddForce(movement, ForceMode.Force);

It’s the functionality of the above script, I want by tilting my phone.
At the moment I’m using the script below, from unity script Ref. But I can’t make it work as intended. The sphere get stuck and won’t roll in the direction I want it to and it starts to bounch randomly.

Vector3 dir = Vector3.zero;
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
    dir.Normalize();

dir *= Time.deltaTime;
transform.Translate(dir * speed);

help with this script would really be appreciated.
Thanks in advance :slight_smile:

This piece of code works a treat.

  void FixedUpdate(){
        Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, 0.0f);
        rigidbody.velocity = movement * speed;
    }

You can get the tilt information of the phone from the accelerometer.
Check http://docs.unity3d.com/Documentation/ScriptReference/Input-acceleration.html

You have to take the orientation of your phone in account though.
http://docs.unity3d.com/Documentation/ScriptReference/Input-deviceOrientation.html

float playerMovingSpeed = 20f;

void FixedUpdate()
    {
        transform.Translate(Input.acceleration.x * playerMovingSpeed * Time.deltaTime, 
                            Input.acceleration.y * playerMovingSpeed * Time.deltaTime, 0);
    }

Was trying to solve this today and found a solution I like a lot better.

if you are working in a situation where all of your rigid bodies need to obey the tilt or you only have one rigidbody, you can (and maybe should) utilize unity’s physics engine as much as you can. For me, i was struggling to create a decent “bounce” effect with a rigidbody on a “rail”, where it would hit the edges of the screen and bounce like a ball. But, I was failing in my physics calculations and it was getting more and more complex. So I thought “why work this hard to emulate gravity when unity already does this?”

Be sure to freeze any rotations or positions on your rigidbody to fit your needs. For me, I froze everything but the Z axis, and my camera is top down. for me, Forward was positive Z. So I set my gravity’s Z value equal to the phone’s acceleration Y value. You will need to adjust how the X Y and Z of the Input.acceleration maps to the X Y and Z values of Physics.gravity.

So for me, I simply set gravity equal to the device acceleration, which is basically what we want

  public Vector3 fakeGravity;

  private void FixedUpdate()
    {
#if !UNITY_EDITOR
       //You may need to adjust how the X, Y, and Z values of each vector map to eachother
        Physics.gravity = Input.acceleration;
#elif UNITY_EDITOR
        //use this to test in editor
        Physics.gravity = fakeGravity;
#endif
    }

One problem with using an accelerometer, is that it exponentially increases in value the more you tilt. It’s not an accurate way of measuring the angle of tilt (that would be gyroscope), but it would be good in a car-type-kind of scenario where, you might want to take hard rights when tilting further than normal.