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 TurboHermit · Oct 14, 2014 at 03:00 PM · rotationmousequaterniondrag

LookTo Rotation Mouse Drag

Hi, I've got a problem that I can't find anywhere. I have a disk that rotates by holding my mouse. The disk constantly points towards the mouse, which is good, however I want it to look like it's being dragged. Meaning, if you click somewhere on the disk it doesn't instantly point towards the mouse but only moves when the mouse moves too, from that point.

I fail to get the wished results, can someone help me?

I'm currently using a raycast to detect the mouse's position and rotate my circle towards hit.point with LookRotation.

Comment
Add comment · Show 2
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 robertbu · Oct 14, 2014 at 03:18 PM 0
Share

Radial drag has been posted a few times UA, so either search out the answers on UA, or alternately post your code and get suggestions on how to solve the problem.

The basic idea is to get the rotation when the mouse go down and then to offset other rotations during the drag by that amount.

avatar image TurboHermit · Oct 14, 2014 at 03:39 PM 0
Share

Yeah that's what I was going for, but I can't seem to find a function that executes that (the offset). I've tried subtracting the starting rotation from the current rotation, but you can't subtract quaternions.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Oct 15, 2014 at 07:10 PM

Here is a Quaternion solution to drag rotate an object. It assuming the camera is looking towards positive 'z' since it rotates around Vector3.forward.

 #pragma strict
 
 private var qStart : Quaternion;
 
 function OnMouseDown() {
     var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     qStart = Quaternion.Inverse(Quaternion.AngleAxis(angle, Vector3.forward)) * transform.rotation;
 } 
 
 function OnMouseDrag() {
     var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward) * qStart;
 }
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 TurboHermit · Oct 15, 2014 at 08:47 PM 0
Share

Robert I love you. I don't really understand the formula yet, but it works and I'm going to take my time to dissect it. Thanks man!

avatar image
0

Answer by unimechanic · Oct 15, 2014 at 04:22 PM

I've tried subtracting the starting rotation from the current rotation, but you can't subtract quaternions

You can subtract the Euler angles:

http://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html

And this could be useful when working with angles:

http://docs.unity3d.com/ScriptReference/Mathf.Repeat.html

http://docs.unity3d.com/ScriptReference/Mathf.DeltaAngle.html

http://docs.unity3d.com/ScriptReference/Mathf.LerpAngle.html

http://docs.unity3d.com/ScriptReference/Mathf.MoveTowardsAngle.html

http://docs.unity3d.com/ScriptReference/Mathf.SmoothDampAngle.html

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 TurboHermit · Oct 15, 2014 at 06:22 PM 0
Share
                 RotateTo.eulerAngles = Quaternion.LookRotation(Vector3.forward,hit.point).eulerAngles - RotateFrom.eulerAngles;
                 transform.rotation = RotateTo;

This is my current code, but it doesn't seem to work the way I imagined it. Shouldn't this produce a drag affect?

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

29 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

Related Questions

Using quaternion for mouse movement? 3 Answers

Quaternion - Euler Angles, weird variable swapping when converting 2 Answers

Keep objects current rotation, with Input Mouse X 0 Answers

allows users to view 360º content without a VR headset 0 Answers

How do I rotate Z axis based off mouse click and drag? 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