Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by jamesfinn180 · Dec 31, 2013 at 02:24 PM · c#rotationmobileaccelerometertransformation

Get Accelerometer to rotate 360 degrees

Hello. I'm using the accelerometer to rotate an object in a game so that the object always points upwards (away from the centre of the Earth let's say) no matter what way the phone is rotated around the z-axis. Think of it like a compass the way the needle always points north no matter what way you have the device rotated. I'm coding in C#.

 using UnityEngine;
 using System.Collections;
 
 public class RotateDial : MonoBehaviour {
     
     private Quaternion localRotation; 
     
     void Start () {
         // copy the rotation of the object itself into a buffer
             localRotation = transform.rotation;
     }
     
     void Update () {
         
         localRotation.z = Input.acceleration.x; 
         
             transform.rotation = localRotation;
     }      
     
     void OnGUI() {
         GUI.Box (new Rect (5, 55, Screen.width / 2, 20), "Local z" + localRotation.z); //, Health_bar_GUI);
         GUI.Box (new Rect (5, 80, Screen.width / 2, 20), "Accel x" + Input.acceleration.x); //, Health_bar_GUI);
         GUI.Box (new Rect (5, 105, Screen.width / 2, 20), "transform rot" + transform.rotation); //, Health_bar_GUI);
     }
 }

My problem is that the accelerometer only seems to work across 180 degrees. The rotation translation for the particular axis is 0 when the phone is held in landscape mode but is either -1 or +1 when it's held portrait (+90 degrees) or upside down portrait (270 or -90 degrees) which works correctly for these rotations. When the phone is held beyond 90 but below 270 degrees the game object does not point in the correct direction. I've attached an image to help explain the problem a bit better.

alt text

I believe it to be because the rotation translation goes back to 0 when the phone is held in upside down landscape mode (180 degrees).

Does anyone know a solution that will allow the game object to keep pointing in the same direction even if the phone is turned completely upside down? Thanks in advance.

accelerometer.jpg (51.2 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Leosori · Jan 04, 2014 at 10:16 PM

You have to use at least the X and Y axis to calculate the rotation. Remember, you are reading gravity force per axis. If you turn your device clockwise, the value on the X axis will raise until 1.0 at 90 degree but then start falling again between 90 and 180 degree. At the same time the value for the Y axis change from negative to positive between 0 and 180 degree.

Starting with landscape mode and rotating around the Z axis, the X,Y values will be:

  • 0° = (0, -1)

  • 90° = (1, 0)

  • 180° = (0, 1)

  • 270° = (-1, 0)

Comment
Add comment · 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
0

Answer by HappyMoo · Dec 31, 2013 at 04:39 PM

My guess is that your acceleration shows the right direction, but the phone changes Input.deviceOrientation, so you get turned around again.

Write out the Angle and the deviceOrientation to a label. If I'm right, add 180 to the angle if the deviceOrientation is on the head.

Comment
Add comment · Show 1 · 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 HappyMoo · Jan 04, 2014 at 10:24 PM 0
Share

did that help?

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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Multiple Cars not working 1 Answer

Limit camera RotateAround for an UAV game 0 Answers

how to stop rotation from being uncontrollable 1 Answer

Using the accelerometer as controller on android. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges