UnityNinja Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

UnityNinja CommunityLog in

UnityNinja - Video Game Development Community, including Resources, Forums, Marketplace & More.


descriptionMatch the tilted angle of phone to object EmptyMatch the tilted angle of phone to object

more_horiz
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?

descriptionMatch the tilted angle of phone to object EmptyRe: Match the tilted angle of phone to object

more_horiz
Are you saying something like set a rotation to the rotation of the iPhone?
If so, then here is a script like that.

Code:

function Update() {
transform.rotation = Input.acceleration;
}
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply