- Home /
Issues with accelerometer calibration?
I am trying to calibrate the accelerometer so that the player can hold the device at any orientation at the start of the game and that becomes the 0 point.
I use this code:
function Update () {
Physics.IgnoreCollision(bomb.collider, collider);
if(!hasInt){
startDir.y = Input.acceleration.y;
startDir.x = Input.acceleration.x;
hasInt = true;
Debug.Log(startDir);
}
//read in accelerometer
dir.x = (-Input.acceleration.y) - startDir.y;
dir.z = (Input.acceleration.x) - startDir.x;
//end read
//Normalize to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
//End normalize
if (startDir.sqrMagnitude > 1)
startDir.Normalize();
}
However, it doesnt change anything which is odd to me. Anyone have any ideas?
Answer by Fraggle222 · May 26, 2012 at 02:22 PM
I use very similar code and it works. Only difference in mine is that I normalize the startDir inside the if(!hasInt) block.
But I don't think that's your problem. So I suspect maybe you aren't actually getting the inputs from a device or something. If you are testing using Unity Remote or something then you might need some code to detect that. Here is what I do (set simulate to true when testing using Unity Remote):
if(simulate || Application.platform==RuntimePlatform.IPhonePlayer ){
//basic calibration:
if(!hasInt){
startDir.x = -Input.acceleration.y;
startDir.y = -Input.acceleration.x;
if (startDir.sqrMagnitude > 1) {
startDir.Normalize();
}
hasInt = true;
Debug.Log(startDir);
}
// remap device acceleration axis to game coordinates
dir.x = -Input.acceleration.y-startDir.x;
dir.z = -Input.acceleration.x-startDir.y;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1) {
dir.Normalize();
}
} else {
dir.z=Input.GetAxis("Vertical");
dir.x=Input.GetAxis("Horizontal");
}
Your answer
Follow this Question
Related Questions
Match the tilted angle of phone to object. 0 Answers
Mapping accelerometer to Camera's position 2 Answers
Accelerometer question 1 Answer
Calabrate Accelerometer 1 Answer
Accelerometer - accounting for gravity for arbitrary device angle 1 Answer