I’m only rotating on Z but this may help you get started:
using UnityEngine;
using System.Collections;
public class RotateTransform : MonoBehaviour
{
public float smooth = 5;
float lastZ = float.PositiveInfinity;
iPhoneOrientation lastKnownOrientation;
void Update()
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
float z = iPhoneInput.acceleration.x * -90;
iPhoneOrientation orientation = iPhoneInput.orientation;
if (orientation == iPhoneOrientation.Unknown)
orientation = lastKnownOrientation;
lastKnownOrientation = orientation;
if (orientation == iPhoneOrientation.PortraitUpsideDown ||
orientation == iPhoneOrientation.LandscapeLeft ||
orientation == iPhoneOrientation.LandscapeRight)
z = 180 - z;
// smooth angle...
if (smooth > 0 lastZ != float.PositiveInfinity)
z = Mathf.Lerp(lastZ, z, smooth * Time.deltaTime);
lastZ = z;
transform.rotation = Quaternion.Euler(0, 0, z);
}
else
{
// keyboard control
float z = Input.GetAxis("Horizontal");
transform.Rotate(0, 0, z);
}
}
}
Basically depending on the orientation, I may translate say 75° to 105°… problem is, there are dead zones, for example between Portrait and LandscapeRight it’ll turn to Unknown, so I try to compensate that by remembering the last known orientation, but it’s not bullet-proof so sometimes you’ll see your object snap. I’m pretty sure it could be more clever so those snaps would occur even less, but when it got satisfactory I just stopped caring ! If I ever end up with a very clean version I’ll put it on the wiki…
And lastly, I smooth the angle a bit otherwise the object jitters (the accelerometer is “too precise”).
Oh yeah and you can ignore the keyboard part if you don’t need it (I have a webplayer version of my app so I just use left/right arrow to simulate tilting the phone).
Some debug code I use to have a clearer view of what’s going on:
It really all depends on the orientation of the phone, and the way an accelerometer works. If you’re standing up with the phone towards you, rotation around the Z-axis (you.transform.forward, ie the direction of your eyes) will be picked up very precisely. You’ll also have good X reading, but nothing on Y.
But if you sit down and lay the phone on a table, you won’t get any interesting input on Z at all! On the other hand, you’ll have excellent X and Y input.
It’s better to think of it as an airplane, I think X and Y are called pitch and yaw, and Z would be roll. For excellent this is the way the Wiimote plugin gives you input.
And you’re right to make your own filter, I forgot to mention it yesterday but I see you’ve read the docs.
I definitely want to try that too, since the orientation given by the OS is not very reliable…
Ahah excellent, j’avais pas remarqué que tu étais Français! Hobbyist? pro? C’est toujours intéressant de connaître les développeurs Unity “du coin”, on n’est qu’une poignée.
</French connection off>
And thanks also for the Wiimote plugin link !
I am use to WiiFlash plugin but did not know that such a plugin exists for Unity ( It’s true that i had not search yet…)
C'est vrai qu'on est pas beaucoup. Mais avec la 2.5, la communautée devrait vite grandir ;-)
En fait, je suis un vieux flasheur totalement converti à Unity. J'ai découvert récemment ce soft, donc je n'ai pas ton expérience mais je progresse tout les jours. J'étais même à unity2008 à Copenhague...
Si tu le souhaites, je serais ravi qu'on fasse plus ample connaissance ;-)
If I use this approach, then in my balancing app, the user will be aware how to maintain balance. But I don’t want user to get the player balanced just by placing the iPhone in particular position.
Actually, I want to spin the gameObject with tilt. More the tilt more the spin to get stoped.