• 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
Question by danielos1996 · Apr 22, 2020 at 07:53 AM · rotationvrquaternionaxisquaternions

How to rotate object based on another rotation.

I am trying to create a tool in VR with a pointer where the user points and an object with the pointer then rotates their hand left and right w$$anonymous$$ch will in turn cause the item that they are pointing at to rotate on a set axis.

I have been working on code to do t$$anonymous$$s but I always have great trouble when it comes to dealing with rotations.

The script I have been working on almost works bar 2 major issues.

Issue one: Quaternion loop issue. T$$anonymous$$s issue arises when the quaternion has made a full turn. Instead of continuing to the next turn seamlessly the interactable item starts rotating in the opposite direction. I would like it to continue on its current direction of rotation and just move onto the next loop.

Issue two: Changing axis messes everyt$$anonymous$$ng up. So most of my testing I was just working on the X axis because I believed if I got that working the other axis would work fine. However I found when switc$$anonymous$$ng axis from x to z for example the rotation would go completely haywire and not rotate any way I would expect it to have.

Any help or advice on t$$anonymous$$s would be really appreciated I have been stuck on t$$anonymous$$s one issue for 28 total hours now. I don’t t$$anonymous$$nk I’ve ever worked on anyt$$anonymous$$ng that has held me up t$$anonymous$$s long. A lot of the issues I’m having are probable down to me not fully understanding quaternions, but they are soooooo confusing.

Here is a pic of how i would like the rotations to work.

And a video link: https://www.youtube.com/watch?v=YWNRu6rT4-A

And a link to the code: https://pastebin.com/dqiHLs5n alt text

example.jpg (274.8 kB)
Comment

People who like this

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

Answer by yummy81 · Apr 22, 2020 at 11:37 AM

I don't know how to create tools in VR. I coded it in plain Unity. At first I was t$$anonymous$$nking about some kind of mapping rotation of the hand into rotation of the target object, but I eventually gave up t$$anonymous$$s idea. I decided to rotate both objects simultaneously and independently, w$$anonymous$$ch still gives the impression that the hand rotates the target. So, I called my script and class Hand. Attach it to your hand object. Drag and drop the object you want to be rotated by your hand into target slot in the inspector. Now if you hold the left mouse button down and move the mouse left and right you will see that hand rotates only around X axis, w$$anonymous$$le target rotates around axis w$$anonymous$$ch you set in the inspector. The target rotates always around one of its local axis, no matter if it is X, Y or Z. Check it out:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Hand : MonoBehaviour
 {
     public Transform target;
     public enum Axis {X, Y, Z};
     public Axis axis;
     public float sensitivity = 100f;
     bool pressed;
     Vector3 vector3;
     
     void Update(){
         
         if (Input.GetMouseButtonDown(0)){
             
             pressed = true;
             
         } else if (Input.GetMouseButton(0) && pressed){
             
             float mouseX = -Input.GetAxis("Mouse X");
             
             transform.rotation *= 
                 Quaternion.AngleAxis(mouseX * sensitivity * Time.deltaTime, Vector3.right);
             
             switch (axis){
                 
                 case Axis.X:
                     vector3 = Vector3.right;
                     break;
                     
                 case Axis.Y:
                     vector3 = Vector3.up;
                     break;
                     
                 case Axis.Z:
                     vector3 = Vector3.forward;
                     break;
                 
                 default:
                     break;
             }
             
             target.rotation *= 
                 Quaternion.AngleAxis(mouseX * sensitivity * Time.deltaTime, vector3);
             
         } else if (Input.GetMouseButtonUp(0)){
             
             pressed = false;
             
         }
     }
 }
Comment
danielos1996

People who like this

1 Show 3 · 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 danielos1996 · Apr 22, 2020 at 12:48 PM 0
Share

That’s awesome! Very helpful thanks so much. That works rotationally exactly how I would like mine to work. I’m having trouble moving the code across though. The only variable that I’m having issues getting I think is the ‘MouseX’ that variable seems to be at 0 when not moving and then increases when moved left and decreases when moved right returning to 0 is still. I was reading on unity what the Input.GetAxis(“MouseX”) returns. It says “If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1...1.” I’m not sure what it means by axis sensitivity or how to replicate that from the hand y rotation.

My Current Code: https://pastebin.com/S3zHAWpp

avatar image yummy81 danielos1996 · Apr 22, 2020 at 02:07 PM 0
Share

I noticed that I was unnecessarily multiplying the value of mouseX by Time.deltaTime. MouseX is frame-rate independent and, in fact it can significantly exceed -1..1 range. It's easy to debug when abruptly moving the mouse. It's very helpful that mouseX get back to zero when not moving. It guarantees that hand stops rotating. If it retained its value, hand would be constantly moving. This sensitivity variable is a factor which controls the speed of rotation. Hand Y rotation? You mean hand rotating around Y axis? Just replace Vector3.right with Vector3.up, just like that:

 transform.rotation *= 
                  Quaternion.AngleAxis(mouseX * sensitivity * Time.deltaTime, Vector3.up);
avatar image danielos1996 yummy81 · Apr 23, 2020 at 07:04 AM 0
Share

Sorry I meant the hand z axis so the user can rotate their wrist to turn the item. I fricken got the sucker. All I needed was the delta angle of the hand z axis. Thanks so much dude could not have done it without looking at your code. I’m no angle nerd so all this delta shiz was way beyond me. So happy to have this working exactly how I want it. My soul can slumber now.

   //Called when pointer hits a valid object.
     internal override void ValidHit(GameObject hit)
     {
         trackedObjLastEuler = trackedObject.eulerAngles;
     }
 
 
     
     //Called every frame that pointer is over valid object.
     internal override void TransformObject(Vector3 rayPos)
     {
 
         if (transformObject == null || trackedObject==null) return;
 
 
 
         Vector3 axisVector = Vector3.zero;
 
 
 
         float handYDelta = trackedObject.eulerAngles.z - trackedObjLastEuler.z;
         if (handYDelta > 180) handYDelta -= 360;
         if (handYDelta < -180) handYDelta += 360;
         trackedObjLastEuler = trackedObject.eulerAngles;
 
 
 
 
         switch (activeAxis)
         {
             case AxisManipulation.x:
                 axisVector = Vector3.right;
                 break;
 
             case AxisManipulation.y:
                 axisVector = Vector3.up;
                 break;
 
             case AxisManipulation.z:
                 axisVector = Vector3.forward;
                 break;
 
             default:
                 Debug.Log("Transform tool issue: No axis chosen to manipulate.");
                 break;
         }
 
 
 
         transformObject.transform.rotation *= Quaternion.AngleAxis(handYDelta, axisVector);
 
 
     }

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Smooth rotation about global axis instead of local axis. 1 Answer

Rotating a 2D sprite to face a target on a single axis. 4 Answers

Quaternion.Slerp on local axis 1 Answer

Problem using Quaternion.AngleAxis around transform.up 1 Answer

Rotation with different speed for different axis 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