I’m trying to recalibrate the accelerometer using a code I found on the Unity Forum HERE -
When I’m trying to call the ‘getAccelerometer’ method inside the Update, I’m getting that error in Unity and it is not making much sense to me as to why it should. Here is the code that I’m using
Matrix4x4 calibrationMatrix;
void calibrateAccelerometer(){
Vector3 wantedDeadZone = Input.acceleration;;
Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0f, 0f, -1f), wantedDeadZone);
//create identity matrix ... rotate our matrix to match up with down vec
Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1f, 1f, 1f));
//get the inverse of the matrix
this.calibrationMatrix = matrix.inverse;
}
Vector3 getAccelerometer(Vector3 accelerator){
Vector3 accel = this.calibrationMatrix.MultiplyVector(accelerator);
return accel;
}
void Start(){
calibrateAccelerometer();
}
void Update(){
Vector3 dir = new Vector3 (getAccelerometer(Input.acceleration));
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
}