- Home /
Accelerometer Axis inverting after collision
Hello all, here is a breakdown of my issue. The game is a 2d top down ball hitting objects game built for Android. Accelerometer controls work fine, however after several minutes of play the axis of the accelerometer controls will invert. I believe that it is linked to collisions where the ball bounces back away from an enemy that it collided with. Additional notes: the controls will work again at the start of the next level, it is an issue that occurs during play. I have tried resetting the control axis on collision, or every frame, even adding in the low-pass and other filters suggested on forums, but this just made the issue more profound (kept low-pass cause it made motion more realistic). This is built and tested using Unity 5.3.2f . The issue has occurred on multiple devices both with remote and as release build. In addition it does not happen with any one enemy nor is it related to their masses as I have tested extensively attempting to remove the issue for nearly 2 weeks. Below is the code used in the controls.
/*here is the snippet for the controls, this is in FixedUpdate function /and as stated works, it is an issue that occurs during gameplay and I have been unsuccssessful in tracking it down other than the bounce back may be causing the flip flop of the axis. */
curAc = Vector3.zero;
LowPassFilterAccelerometer();
curAc = new Vector3(lowPass.x, lowPass.y, 0f);
if(curAc.sqrMagnitude >1){
curAc.Normalize();
}
curAc *= Time.fixedDeltaTime;
transform.Translate(curAc*speed);
}
}
//This is the low pass filter, which works fine.
void LowPassFilterAccelerometer(){
lowPass = Vector3.Lerp(lowPass, Input.acceleration, filterFact);
}
//this is the reset that was added to the OnCollisionEnter.
void ResetAxes(){
curAc = Vector3.zero;
}
Any help would be greatly appreciated.
Answer by kloudon716 · Apr 03, 2016 at 10:21 PM
In case anyone else runs into this, the issue is caused by the collision causing a 3D rotation in the z axis, despite all colliders being 2d. To remove the issue on the player's Rigidbody 2d -> constraints check to turn on z-rotation (there should be a check in the box). This will stop the axis from rotating with the accelerometer inputs as the player can no longer rotate in z.
Your answer
![](https://koobas.hobune.stream/wayback/20220612064230im_/https://answers.unity.com/themes/thub/images/avi.jpg)