Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » FREE Project Downloads & Recources » Scripts » Match the tilted angle of phone to object

Match the tilted angle of phone to object

Go down  Message [Page 1 of 1]

1 Match the tilted angle of phone to object on Thu Oct 01, 2015 12:06 pm


Member [Lv. 0]
Member [Lv. 0]

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

View user profile

2 Re: Match the tilted angle of phone to object on Sun Oct 11, 2015 8:43 pm


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;

View user profile

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum