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
0
Question by Who · Dec 16, 2013 at 01:29 AM · cameraquaternionfirst person controllergyro

Given a quaternion, how do I modify it to zero rotations about certain axes?

I have been struggling with this problem for weeks so I finally decided to seek help. I am working on a script for controlling a first person character using the gyro. I found the iPhone script by Perry Hoberman and I have since converted it to C# and made modifications to it to suit my purpose...mostly by trial and error ;) The problem I am trying to deal with is that I need the gyro to only control the vertical rotation(x-axis) of the camera and only the horizontal rotation (y-axis) of the parent of the camera. The following is the update function code that I am using to get the quaternion for the camera rotation:

 void Update () {
         rotation = Input.gyro.attitude * new Quaternion(0f,0f,0.7071f,0f);
         transform.localRotation = rotation;
     }

I have confirmed that this works even though the w component of the rhs quaternion is strangely 0. Without the rhs quaternion, rotations are inverted where tilting the phone up causes the camera to look down and I believe the other axes were swapped.

What I now need to do is to isolate only the x-axis of the rotation quaternion for the camera and the y-axis for the parent. Converting the quaternion to euler and setting the unwanted axes to zero does not work. It does restrict rotation to the wanted axis but rotation on that axis gets limited to a 180 degree range if I remember correctly.

I am new to Unity and quaternions so if I am going about this in the wrong way please let me know and any help with getting the gyro to work as a first person controller would be greatly appreciated.

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
0

Answer by tanoshimi · Dec 16, 2013 at 07:27 AM

Quaternions don't have an x-axis of rotation- if you're working in that frame of reference you need to be using Euler angles instead. What makes you say that would limit the range of rotation to 180 degrees? It should work fine by creating a Vector3 of the eulerAngles, zeroing the z parameter, and reassigning to the transform.rotation.

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 Who · Dec 16, 2013 at 09:53 AM 0
Share

I understand that quaternions don't have an x-axis and I guess what I really want is a quaternion that is equivalent to the Euler rotation without the y and z components. I use this to output the Euler values on the screen to try to figure out what is happening:

 GUI.TextArea(new Rect(100,0,300,50),"X"+rotation.eulerAngles.x+" Y:"+rotation.eulerAngles.y+" Z:"+rotation.eulerAngles.z)


When my phone is facing south and to the floor, the x value is 360 and it goes down to 270 as I tilt upwards to facing forward but the confusing thing is that as I tilt further to point to the ceiling in the same direction, the value starts to go back up to 360. Now obviously if I use those Euler values to recreate a quaternion for the rotation, I would get correct rotation from facing the floor to facing forward but as I tilt to the ceiling the camera starts to tilt back to the floor. Also the range of x values changes from 0 to 90 and back to 0 if I am facing north.

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

17 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

Related Questions

Gyro quaternion offset 1 Answer

3D camera relatively using gyro a la N.O.V.A. 2 0 Answers

Camera Rotation using Gyroscope: Yaw and Pitch reversed 2 Answers

Quaternion partial offset, need help badly 0 Answers

"moveable" first person controller 0 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