Moving character in Unity3D based on accelerometer input I am building a infinite vertical platformer for mobile platforms using Unity3D. I am using the accelerometer to move the character left and right on the screen.

if (Input.acceleration.y &gt; -0.2f &amp;&amp; Input.acceleration.y &lt; 0.2f) {
                        maxSpeed = 17;
                }

        if (Input.acceleration.y &gt; -0.5f &amp;&amp; Input.acceleration.y &lt; -0.2f) {
            maxSpeed = 25;
        }
        if (Input.acceleration.y &gt; 0.2f &amp;&amp; Input.acceleration.y &lt; 0.5f) {
            maxSpeed = 25;
        }
        if (Input.acceleration.y &gt; -0.7f &amp;&amp; Input.acceleration.y &lt; -0.5f) {
            maxSpeed = 25;
        }
        if (Input.acceleration.y &gt; 0.5f &amp;&amp; Input.acceleration.y &lt; 0.9f) {
            maxSpeed = 25;
        }
        if (Input.acceleration.y &lt; -0.9f) {
            maxSpeed = 40;
        }
        if (Input.acceleration.y  &gt; 0.9f) {
            maxSpeed = 40;
        }


Basically the farther the device is tilted the faster the character moves. I'm pretty sure there is a way better way to write this code but I'm pretty terrible at math and I can't figure it out. Any suggestions on cleaning this up a bit?
I know absolutely nothing about c# or unity3d, so if there's a language specific assist, I have no idea about it!

I notice each condition checks Input.acceleration.y, so that's one bit of information that repeats. I also notice we have three speeds, yet we have seven conditionals!

First of all, the negatives are confusing. Find the absolute value first.

I'm going to assume a number followed by an "f" means float. So maybe we need something like:

float acceleration = Math.Abs(Input.acceleration.y);


Great, now it's going to be easier to handle this!

Now all we need is a simple check like so:

if (acceleration &lt; 0.2f) {
    maxSpeed = 17;
} else if (acceleration &lt; 0.9f) {
    maxSpeed = 25;
} else {
    maxSpeed = 40;
}


Your original numbers were a little weird, but if you meant for such a strange pattern, correct me!