Hi,
I am trying to match an object rotation to the titled angle of the phone.
I found a great script for this here:
answers*unity3d*com/questions/139294/match-the-tilted-angle-of-phone-to-object.html#answer-form
This is the code:
private var sizeFilter: int = 15;
private var filter: Vector3[];
private var filterSum = Vector3.zero;
private var posFilter: int = 0;
private var qSamples: int = 0;
function MovAverage(sample: Vector3): Vector3 {
if (qSamples==0) filter = new Vector3[sizeFilter];
filterSum += sample - filter[posFilter];
filter[posFilter++] = sample;
if (posFilter > qSamples) qSamples = posFilter;
posFilter = posFilter % sizeFilter;
return filterSum / qSamples;
}
function Update() {
transform.up = -MovAverage(Input.acceleration.normalized);
}
The problem is that when holding the android device face-up and titling it right and left, the object does not move only right and left, it also moves a little bit diagonal (up/down). can someone help improve the code so it rotates the object exactly according to the rotation of the android device even when the device is face-up and titlted right/left?
I am trying to match an object rotation to the titled angle of the phone.
I found a great script for this here:
answers*unity3d*com/questions/139294/match-the-tilted-angle-of-phone-to-object.html#answer-form
This is the code:
private var sizeFilter: int = 15;
private var filter: Vector3[];
private var filterSum = Vector3.zero;
private var posFilter: int = 0;
private var qSamples: int = 0;
function MovAverage(sample: Vector3): Vector3 {
if (qSamples==0) filter = new Vector3[sizeFilter];
filterSum += sample - filter[posFilter];
filter[posFilter++] = sample;
if (posFilter > qSamples) qSamples = posFilter;
posFilter = posFilter % sizeFilter;
return filterSum / qSamples;
}
function Update() {
transform.up = -MovAverage(Input.acceleration.normalized);
}
The problem is that when holding the android device face-up and titling it right and left, the object does not move only right and left, it also moves a little bit diagonal (up/down). can someone help improve the code so it rotates the object exactly according to the rotation of the android device even when the device is face-up and titlted right/left?