How to make move the player using the android device tilt

Hello everybody!

I really need your help cause I’m trying to create an android game based on the space shooter example of Unity, and I need to move the spaceship(player) using the tilt of the device. This is the code that I have used, but it doesn’t work… (the spaceship only rotates but doesn’t move along the x and z axes as I want…)

Please help mee

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
	public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour
{
	public float speed;
	public float tilt;
	public Boundary boundary;


	void FixedUpdate ()
	{
		float moveHorizontal = Input.acceleration.x;
		float moveVertical = -Input.acceleration.z;
		
		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
		rigidbody.velocity = movement * speed;
		

		transform.position = new Vector3 
			(
				Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
				0.0f, 
				Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
				);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
	}
}

In the Unity manual there’s this code but I don’t know how to use it (well I 've tryed but it didn’t work…)

transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);

Are you sure you’re not accidentally multiplying speed by 0? I know a lot of people put unassigned public floats and ints in their scripts and then the editor initializes them as 0’s. Are you getting bit by this in your playerController class there?

What is the

transform.position = new Vector3 (
          Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
          0.0f, 
          Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
          );

for?

If you have a rigid body that is not set to isKinematic you shouldn’t try to move the gameobject.
Just apply forces to the rigidbody. I see that you are trying to directly change the velocity of it.

I’ve found out that the problem is another: the code works perfectly when i’m using unity remote , but when I build the game and install it on my device the spaceship doesn’t move and shoot… I’ve looked for a solution on the forum and unity answers but i havent found nothing useful… Can you give some pieces of advice?

I think you should be moving it on X and Y axis rather than X & Z