Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 chilpert · Apr 30, 2020 at 09:26 AM · rotationquaterniongyroscope

How to implement a mounted but movable headlight using the gyroscope?

Hello,

I have a vehicle with a headlight mounted on top of it. If the vehicle takes a left turn, so will the headlight. However, a player should still be able to move the headlight using the gyroscope. My phone app sends the attitude member of the gyroscope class provided by unity (which is a quaternion for the absolute orientation) to my desktop computer. My guess was to first convert the handedness to work with Unity and then assign the attitude to the headlight's rotation. However simply assigning it like this will override the effect of the headlight moving with the vehicle, which I achieved by simply making the headlight a child of the vehicle itself.
Another issue I have to take care of is to add a calibration process which makes sure after calibration, your phone and the headlight are aligned. The way I want the player to hold his/her phone is in landscape mode with the camera pointing forward. Where the camera is looking is therefore the viewing direction. I have tried to add a calibration step by first letting the user hold his phone in landscape with the camera facing towards the screen and then press a button (to take a sample value of the current rotation) and then holding the phone in a way that the headlight in game is pointing straight (to take the final sample value). Then I calculate the offset between both quaternions using the inverse and finally multiplying the current rotation by the offset in the correct order. However the result is not correct. Up and down in game are more like rotating the phone from top left to bottom right. Left and right are similarly wrong.

I have been stuck with these issues for a long time now. I hope someone will be able to help me with it.
Best regards!

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
Best Answer

Answer by chilpert · Apr 30, 2020 at 05:27 PM

I was able to get something that works most of the time. This is my implementation in case you are trying to solve the same problems.

 public class GyroscopeController : MonoBehaviour
 {
   [SerializeField]
   private GameObject vehicle = null;
   [SerializeField]
   private GameObject headlight = null;
 
   private Quaternion rotation;
   private Quaternion offset;
 
   private void Start()
   {
     rotation = new Quaternion();
     offset = Quaternion.identity;
   }
 
   private void Calibrate()
   {
     Debug.Log("Gyroscope Controller: Calibrating phone");
     offset = Quaternion.identity * Quaternion.Inverse(rotation);
   }
 
   public void UpdateGyroscope(Quaternion rot, ref bool calibrate)
   {
     rotation = GyroToUnity(rot);
 
     if (calibrate)
     {
       Calibrate();
       calibrate = false;
     }
 
     rotation = vehicle.transform.rotation * offset * rotation;
     headlight.transform.rotation = rotation;
   }
 
   private static Quaternion GyroToUnity(Quaternion q)
   {
     return new Quaternion(q.x, q.y, -q.z, -q.w);
   }
 }
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

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

165 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gyroscope rotation offset 1 Answer

Virtual Reality - Android Gyroscopic Camera - Pitch and Roll Issues. 1 Answer

gyro attitude different w.r.t to app open 1 Answer

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

How to obtain roll / tilt angle from gyroscope? 3 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