• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DasFloX · Mar 09, 2015 at 10:09 AM · androidrotationgyroscopeaccuracy

Android Gyroscope inaccuracy problems.

Hi, i'm making a game for android, where you are controlling a car from above and by rotating the phone you rotate the car in the other direction, so that it looks like the car is always facing the same direction in real world. I'm using the gyroscope for this purpose.

 Input.gyro.enabled = true;
 Input.gyro.updateInterval=0.01f;
 

Problem is, that both methods i was trying to use are really inaccurate.

First i tried to add the rotationRate to the current rotation.

 player.transform.localEulerAngles += new Vector3 (0, Input.gyro.rotationRate.z, 0);

When i start the game, the player will rotate faster then it should. When i turn my phone 100° the player performs a full 360° turn. Maybe im missing out on some reference to the framerate. So i tried getting some multiplication value, by dividing Time.deltaTime with the updateInterval.

 player.transform.localEulerAngles += new Vector3 (0, Input.gyro.rotationRate.z, 0)* (Time.deltaTime/Input.gyro.updateInterval);


But that made the car rotate even faster and out of controll or if i switch updateInterval and deltaTime it doesnt even move.

My other approach was setting it directly to the rotation of the device.

 player.transform.localEulerAngles = new Vector3 (0, Input.gyro.attitude.eulerAngles.z, 0);

That works for a short momentan, but then the results are getting incredibly inaccurate, having the car skip more than 10° on fast movements.

I dont quite know wich of those is the best solution but since adding the rotationRate proved to be the smoother option i would like to try it that way, but i dont know with what to multiply it to my desired result.

Thanks in advance for any help, FlomoN

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Pendantic · Mar 10, 2015 at 04:20 PM

I would recommend sticking with gyro.attitude but possibly adding a lerp to smooth out those jumps you have. Also if you dealing with angles you may not want to convert to euler as there can be some weird issues with that. It looks like you want the z rotation of the device to set the Y rotation which is a little tricky but can be done. This is how you could convert it

 //this returns the direction use Vector3.right if Vector3.up doesn't work
 Vector3 direction = Input.gyro * Vector3.up;
 //this just will take away any up or down rotation
 direction.y = 0;
 Quaternion rot = Quaternion.LookRotation(direction,Vector3.up);
 transform.rotation = Quaternion.Lerp(transform.rotation,rot,5f*Time.deltaTime);

 
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DasFloX · Mar 10, 2015 at 05:59 PM 0
Share

Okay main-reason i used euler is that i don't understand how Quaternion works and Quaterion also seems very abstract to me. One more problem is, that the player needs to have a certain starting rotation (-90, 90, 0) and i dont know how to apply that to the Quaternion.

I tried this

 rot += new Quaternion(0.5f, -0.5f, 0.5f, 0.5f);

that quaternion seems to be my needed rotation according to some online converter, but it says, that you can't add quaternions together.

avatar image Pendantic · Mar 10, 2015 at 06:10 PM 0
Share

you may want to parent you gameobject so that it's initial world world rotation is 0,0,0 then in start get your initial gyro quaternion as above and then divide any further rotations you get by that initial value. With quaternions multiplying is equivalent to adding and dividing is equivalent to subtraction, don't ask me why

avatar image DasFloX · Mar 10, 2015 at 07:50 PM 0
Share

Doesn't really work, when i multiply that value the car is still facing the same direction as before.

avatar image Pendantic · Mar 11, 2015 at 01:04 AM 0
Share

Hmm can I can I see what you wrote?

avatar image DasFloX · Mar 11, 2015 at 01:24 PM 0
Share
 Vector3 direction = Input.gyro.attitude * Vector3.up;
         //this just will take away any up or down rotation
         direction.y = 0;
         Quaternion rot = Quaternion.LookRotation(direction,Vector3.up);
         rot *= new Quaternion(0.5f, -0.5f, 0.5f, 0.5f);
         player.transform.rotation = Quaternion.Lerp(transform.rotation,rot,5f*Time.deltaTime);
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input.gyro.attitude not working on newer Android devices 3 Answers

Gyroscope smooth transitions (minimizing jerk from tiny movements) 1 Answer

Gyroscope.userAcceleration returning zero 0 Answers

Android gyroscope 4 Answers

How to setup android build so that users without gyroscope do not see it in Play Store ? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges