• 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
0
Question by bottus · Nov 03, 2014 at 05:26 PM · 2drotationspritetouchvector2

2 fingers sprite rotation

Hey guys !

I'm reproducing touch gesture my sprite .. I already did the double tap to zoom in, the pinch and the drag. I'm currently working on the rotation using two fingers. I already recognize the fingers, get their position and try to interpret it correctly but so far i didn't get anything real correct ..

Here is what u did :

 private void HandleRotation()
     {
         var f1 = new Touch();
         var f2 = new Touch();
         for (var i = 0; i < Input.touchCount; i++)
         {
             var wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
             var touchPos = new Vector2(wp.x, wp.y);
             if (collider2D == Physics2D.OverlapPoint(touchPos))
             {
                 if (this._fingerIndex == 0)
                 {
                     this._fingerIndex += 1;
                     f1 = Input.GetTouch(i);
                 }
                 else
                 {
                     f2 = Input.GetTouch(i);
                 }
             }
         }
         if (f1.phase == TouchPhase.Moved || f2.phase == TouchPhase.Moved)
         {
             var turnAngleF1 = Vector2.Angle(f1.position, f1.deltaPosition);
             var turnAngleF2 = Vector2.Angle(f2.position, f2.deltaPosition);
             var turnAngle = (turnAngleF1 + turnAngleF2) / 2;
             Debug.WriteLine(turnAngle);
             transform.Rotate(0, 0, turnAngle * 0.04f);
         }
     }

I figured that when you rotate something with your two fingers, you two fingers does not move at the same speed i then try to ponder the result by adding the two of them and divide the result by 2.

The weird thing is that my turn angle is ALWAYS between 90 and 145, that's the first thing i don't really get since my finger are almost not moving.

Second thing i'd like to ask is, how do i figure if my rotation is clockwise or not ?

Thank you for reading and helping. Bottus.

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 · Nov 03, 2014 at 05:29 PM 0
Share

I don't have time for a detailed answer for the moment, but I want to suggest you handle it this way:

  • Save the finger positions from the previous frame

  • Generate a vector for both the previous frame and the current frame by subtracting f1 from f2.

  • Use 'Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg' to calculate a signed angle for each vector. Note that the parameters to Atan2 are y,x.

  • Rotate your object by the delta angle between the two angles.

avatar image bottus · Nov 03, 2014 at 05:34 PM 0
Share

$$anonymous$$mh i don't really get what you are proposing ..

1 Reply

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

Answer by robertbu · Nov 03, 2014 at 06:37 PM

Untested, but I'm suggesting you replace line 24 - 28 with the following:

 var prevPos1 = f1.position - f1.deltaPosition;  // Generate previous frame's finger positions
 var prevPos2 = f2.position - f2.deltaPosition;
 
 var prevDir = prevPos2 - prevPos1;      
 var currDir = f2.position - f1.position;

 var angle = Vector2.Angle(prevDir, currDir);

 transform.Rotate(0,0,angle);  // Rotate by the deltaAngle between the two vectors

Note that Vector2.Angle() should be signed. If not you can use Mathf.Atan2() for each of pervDir and currDir and rotate by the difference. Also, rather than generate the previous finger positions, you can just save them from the previous frame.

Comment
Add comment · Show 2 · 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 mnml_ · Oct 09, 2015 at 12:50 PM 0
Share

pervDir chuckle

avatar image Marc477 · Dec 02, 2020 at 12:35 AM 0
Share

you should use this function instead: var angle = Vector2.SignedAngle(prevDir, currDir);

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to rotate my 2d gameObject using touch input?? 1 Answer

2D Sprite Rotation 0 Answers

Rotating a Sprite to face mouse / target? 2 Answers

Trying to rotate a 2D sprite 2 Answers

Weird translatation after rotation 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