• 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 GameDevH2O · Apr 26 at 01:23 AM · rotation3dquaternionrotatereset

How to Reset Quaternion/Rotation On 3D Object After Dragging Mouse?

Hello, I have an issue with resetting my gameobject's rotation. The rotation is based off mouse drag with restrictions. I have it to when the mouse is released the object returns to its starting rotation. But when I re-drag the object again the object rotation starts from where it was left off at from the previous drag. When I need it to drag from its starting rotation. I've tried many different methods nothing seems to work.

 public Camera cam;
 public float speed, minEulerX, maxEulerX, minEulerY, maxEulerY;
 float eulerX, eulerY, rotateY, rotateX, inputX, inputY;
 Quaternion startrot;
 bool touch;

 void Start()
 {
     startrot = transform.rotation;
 }

 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, Object))
         {
             inputX = Input.mousePosition.x;
             inputY = Input.mousePosition.y;
             touch = true;
         }
     }

     if (Input.GetMouseButton(0))
     {
         if (touch)
         {
             rotateX = -(Input.mousePosition.y - inputY) * speed;
             rotateY = -(Input.mousePosition.x - inputX) * speed;
             eulerX = Mathf.Clamp(eulerX + rotateX, minEulerX, maxEulerX);
             eulerY = Mathf.Clamp(eulerY + rotateY, minEulerY, maxEulerY);
             transform.rotation = Quaternion.Euler(-eulerX, eulerY, 0) * startrot;
             inputX = Input.mousePosition.x;
             inputY = Input.mousePosition.y;
         }
     }
 
     if (Input.GetMouseButtonUp(0))
     {
         transform.rotation = startrot;
         touch = false;
     }    
 }
 
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
0
Best Answer

Answer by stalwart5033 · May 09 at 10:56 PM

Set zero the eulerX and eulerY values after rotating

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 GameDevH2O · May 10 at 02:53 AM 1
Share

Project saver thank you.

avatar image
0

Answer by Hawaii_Dev · Apr 28 at 07:27 AM

You have to set your inputX and inputY to zero so to not start the offset at a weird angle. Insert into your if(Input.GetMouseButtonUp(0)) function: inputX = 0; inputY = 0;

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 GameDevH2O · Apr 28 at 06:18 PM 0
Share

hello, I tried your recommended answer & im getting the same results for some reason

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

185 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

Related Questions

How to smoothly rotate an object on only two axes? 2 Answers

Snap and align two cubes 1 Answer

How get the rotation of an GameObject? 3 Answers

Rotate object to target position 1 Answer

Clamped Rotate 3D Object on Y and Z axis only with OnMouseDrag() 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