• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by ChrisBliss · Dec 14, 2012 at 01:40 PM · drag

How to drag a globe

Hi, I have actually found several topics that come close to my question but I still could't find a solution for my problem in other Threads here or general google search.

I have created a physicalized sphere (The Earth) and want do drag a point in order to then move the mouse/my finger in order to rotate (not spin) the sphere.

I'm shooting a Raycast through the viewport onto the sphere to get the points on it for my calculation. That works!

I can also calculate the rotation of the earth via two points on it. startPoint gets set on the first frame of the touch/click. It won't change until I click again. hit.point in the TocuhHold function updates on every frame. That works, too!

PROBLEM:

startPoint = hit.point; sets both points to be the same which has the result that in the calculation below the Quaternion is (0.0, 0.0, 0.0, 0.0). I'm aware of that because both points are the same.

This results in the earth alwas snapping back to "zero-rotation"

I cannot ADD the quaterion from the TouchHold to the Quaternion the earth has aftzer I dragged it.

I tried to multiply the Quaternion instead of setting it directly to the FromToRotation but that spins the earth very fast since it multiplies every frame although I read that *= with Quaternion means "combine". Still I cannot get my head around what that means.

I also tried several other stupid things but all comes down to the earth snapping back.

So how can I preserve the roation after a drag so that the next time I drag it it won't reset it's rotation but actually rotate "further". Think "google earth"

And sorry for the long text for a maybe VERY simple solution. But I don't see it.

Here' the whole script:

     public class Earth : MonoBehaviour {
     
         private RaycastHit hit;
         private Vector3 startPoint;
         private Vector2 touchPos;
         private Vector2 mouseViewportPos;
         public Transform earth;
     
     
     
         void Update ()
     
     [...]"Here's code that calls the TouchDown, -Hold and -End Function. 
     
     They work so no need to spam this here"[...]
     
     
     
         void TouchDown ()
         {
             RayCast();
             startPoint = hit.point;
         }
     
         void TouchHold ()
         {
             RayCast();
             earth.transform.rotation = Quaternion.FromToRotation(startPoint, hit.point);
         }
     
         void TouchEnd ()
         {
     
         }
     
         void RayCast ()
         {
             Ray ray = Camera.mainCamera.ViewportPointToRay(touchPos);
     
             if (Physics.Raycast(ray, out hit, 100))
             {
                 Debug.DrawLine(ray.origin, hit.point, Color.red);
                 Debug.DrawLine(ray.origin, startPoint, Color.blue);
             }
         }
     
     
 
     }
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

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Grab and throw object immediately 1 Answer

Drag an object from the start of a path to end 1 Answer

Can I force z position to match prefab when it's dragged onto scene? 1 Answer

Drag Question 1 Answer

how i can drag a sprite in 2D mode?? 1 Answer


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