- Home /
Buggy Android accelerometer code
Hi all!
I need help regarding android accelerometers.
I am working on a bike game atm, and I have everything nice and setup, animation, physics, movement, etc. But there is one problem. I have the controls setup such that when you tilt your android device up or down, it moves forward, and left and right it turns. The problem lies in the turning mechanism. If I hold down the device at a corner for too long, it glitches out and stays stuck in that turning motion.
Here is the code for the control mechanism:
public static float tiltThreshhold = .175f;
public float moveSpeed = 4f;
public float rotateSpeed = 3f;
public static bool deadzone = false;
private float panLimit = .9f;
private float filter = 3f;
public static Vector3 accel;
public static float tiltCompare = .5f+accel.y;
void Start () {
accel = Input.acceleration;
}
// Update is called once per frame
void Update () {
Move();
}
void Move()
{
accel = Vector3.Lerp(accel,Input.acceleration,filter*Time.deltaTime);
var accelerometerDir = new Vector3(0,0,.5f+accel.y*1.1f);
var gravityDir = new Vector3(0,-5,0);
var RotDir = new Vector3(0,accel.x,0);
if(accelerometerDir.sqrMagnitude > 1) accelerometerDir.Normalize();
transform.Translate(accelerometerDir*moveSpeed*Time.deltaTime);
transform.Rotate(RotDir*rotateSpeed*Time.deltaTime);
Debug.Log(accel.ToString());
}
Please let me know asap! Also please let me know if I need to be more specific, or even show screenshots or video of what is going on. Thank you for your time
p.s. I am using unity 3.5 and due to financial reasons for licensing, wont be upgrading to unity 4 yet
upon further testing and research, I've been able to conclude one thing. The system works for a certain amount of time, but afterwards it glitches out. This is probably due to some weird issue where the accelerometer input is not being registered after that point. so the accelerometer works for 30 seconds maybe, but afterwards the system locks up and I can't use the accelerometer to move my bike. hopefully someone will be able to tell whats going on here
Your answer
Follow this Question
Related Questions
accelerometer speed 1 Answer
Player Movement when phone shakes 0 Answers
Android Glitch - random assets display after splash screen -1 Answers
android accelerometer left right 3 Answers
Weird double-tap bug when I use standart controller (Android) 0 Answers