Hi all, it’s my first post in Unity community forum:)
I’m currently trying to make my ball moving by using gyroscope. I want to use almost the same movement logic as i used in PC movement. On PC it works well.However, on mobiles i can move my ball only up and down. I have no idea what is wrong in my code, please, help me.
That’s a piece of PlayerController script, that matters:
void Update ()
{
Rigidbody rigidbody = transform.GetComponent<Rigidbody>();
Vector3 direction = Vector3.zero;
//PC code works great
if (SystemInfo.deviceType == DeviceType.Desktop)
{
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
direction = -Vector3.left;
}
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
direction = Vector3.left;
}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
direction = Vector3.forward;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
direction = -Vector3.forward;
}
rigidbody.AddTorque(direction * speed);
}
//My ball moves only up and down, no left or right
if (SystemInfo.deviceType == DeviceType.Handheld)
{
float x = Input.acceleration.x;
float y = Input.acceleration.y;
if (x > 0)
{
direction = -Vector3.forward;
}
if (x < 0)
{
direction = Vector3.forward;
}
if (y > 0)
{
direction = -Vector3.left;
}
if (y < 0)
{
direction = Vector3.left;
}
rigidbody.AddTorque(direction * speed);
}
}