FragMate CommunityLog in

FragMate - Software and Game Developer Community, including Resources, Forums, Marketplace & More.


descriptionMatch the tilted angle of phone to object


I am trying to match an object rotation to the titled angle of the phone.

I found a great script for this here:

This is the code:
private var sizeFilter: int = 15;
private var filter: Vector3[];
private var filterSum =;
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?

descriptionRe: Match the tilted angle of phone to object

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


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