Tilting on Android phone doesn't work

Hi,
I have problem with tilting on Android which should move with the object. Here is simple code:

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (isAI)
        {
            AIControl();
		}
		else
		{
			PlayerControl();
        }
    }

    private void PlayerControl()
    {
#if UNITY_PLATFORM_ANDROID
        playerMove = new Vector2(0, Mathf.Sign(Input.acceleration.y));
#else
		playerMove = new Vector2(0, Input.GetAxisRaw("Vertical"));
#endif
    }

    private void AIControl()
	{
        if (ball.transform.position.y > transform.position.y + 0.5f)
        {
            playerMove = new Vector2(0, 1);
        }
        else if (ball.transform.position.y < transform.position.y - 0.5f)
        {
            playerMove = new Vector2(0, -1);
        }
        else
        {
            playerMove = new Vector2(0, 0);
        }
	}

    private void FixedUpdate()
	{
		rb.velocity = playerMove * movementSpeed;
	}

On Windows is everything fine, code is working. Do you have any idea what’s wrong?
Thanks, Peter

1 Like

Nope, but you can via the wonderful process of debugging!

I suggest starting from the acceleration data (from Input) to make sure you aren’t missing some critical permission required by your phone.

If it’s not working there, start checking adb logcat and seeing if there’s any errors.

If you’re getting good acceleration numbers on the phone, follow the data through the rest of your code.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Based on Unity docs your compiler directive is not correct.
Use UNITY_ANDROID platform scripting symbols instead. I use it in my Sensor Camera asset and it works.

1 Like

You are absolutely right, it really works with this symbol - UNITY_ANDROID. Thanks a lot. :wave:

1 Like