• 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 AzzyDude24601 · Jan 18, 2021 at 04:02 PM · rotationquaternioneulerangles

Apply game object rotation using relative VR controller rotation?

I have a game object which I want to be able to rotate using my VR controller. I can kind've achieve this by doing the following...


     bool triggerClicked = false;
     Transform rightHand = Player.instance.rightHand.transform;
 
     void Update () {
         if (triggerClicked) {
              transform.eulerAngles = rightHand.eulerAngles;
         }           
     {


...but the problem is the object I want to rotate initially snaps to my controller's current rotation when I press the trigger. I need it to move relative to the object's initial rotation instead. What is the correct way to do this?

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
1

Answer by AzzyDude24601 · Jan 18, 2021 at 05:32 PM

Solved it. You need to capture some initial quaternions when the trigger is first clicked and then calculate the relative differences and apply them. This prevents the initial snapping to rotation issue.


   private bool triggerClicked = false;
   private Transform rightHand = Player.instance.rightHand.transform;

   private Quaternion initialObjectRotation;
   private Quaternion initialControllerRotation;

   private bool set = false;
 
  void Update () {
      if (triggerClicked) {

          if(set == false)
             {
                 initialObjectRotation= transform.rotation;
                 initialControllerRotation = rightHand .rotation;
                 set = true;
             }

             Quaternion controllerAngularDifference = initialControllerRotation * Quaternion.Inverse(rightHand.rotation);
             transform.rotation = controllerAngularDifference * initialObjectRotation;
      }         
      else
      {
          set = false;
      }
  {


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 RhituTime · 4 days ago 0
Share

Thanks, I can confirm this worked. Can be simplified a bit, instead of two initialRotation variables, their combined state can be saved in one Quaternion relativeRotation like this:

 relativeRotation = Quaternion.Inverse(rightHand.rotation) * transform.rotation;

And the resulting object rotation set like:

 transform.rotation = rightHand.rotation * relativeRotation;

Needed like 1000 attempts to figure it out until I ended up with the same solution like yours, then it finally snapped together.

avatar image
0

Answer by xogur6889 · Jan 18, 2021 at 04:57 PM

Vector3 firstEuler;

void Start() { firstEuler = transform.eulerAngles; }

update() { ... { transform.eulerAngles = firstEuler + rightHand.eulerAngles; } }

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 AzzyDude24601 · Jan 18, 2021 at 05:03 PM 0
Share

This is still causing an initial "snap" of the rotatable object? As in it jumps to a new initial rotation and then works as intended.

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

154 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

Related Questions

Rotate with raw gyro data. 0 Answers

Rotating a character relative to the ground's rotation 1 Answer

How to handler rotation of a Gameobject while aligning it with normals 1 Answer

How do I clamp the Z-Axis Rotation for this code? 1 Answer

How do I make create a rotateable object that follows a raycast hit point? 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