Hi,
I’m making a basic ball maze for android and have been looking at the c# game example on the asset store. On pc it works fine but my android controls dont seem right…the left/right tilt seems backwrds and forwards/backwards is completely messed up resulting in the ball floating and bobbing around…
I want to be able to tilt the phone and roll the ball around the maze, any help is much appreciated!
Thanks.
using UnityEngine;
using System.Collections;
public class MarbleControl : MonoBehaviour {
public float movementSpeed = 6.0f;
void Update () {
Vector3 movement = (Input.GetAxis("Horizontal") * -Vector3.left * movementSpeed) + (Input.GetAxis("Vertical") * Vector3.forward *movementSpeed);
rigidbody.AddForce(movement, ForceMode.Force);
//Android controls
// controller = GetComponent(CharacterController);
Vector3 dir = Vector3.zero; //Android controls - accelorometer
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
// dir.y = 0;
//clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
//move at meters/second instead of per frame
dir *= Time.deltaTime;
transform.Translate(dir * movementSpeed);
//move ball
//controller.Move (dir * movementSpeed);
}
void OnTriggerEnter (Collider other ) {
if (other.tag == "Pickup")
{
MarbleGameManager.SP.FoundGem();
Destroy(other.gameObject);
}
else
{
//Other collider.. See other.tag and other.name
}
}
}
I didn't think the majority of phones/tablets have gyroscopes now, that they opt for accelerometers instead? Or would the function work anyway, do you have any code working with the gyroscope? On a side note, if i fix the Y position of the ball's rigidbody it seems to work ok, but I need the ball moving along all 3 axis to go up/down ramps and such.
– C-Blunt