Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Belevar · Dec 20, 2017 at 03:02 PM · rotationinputunity 2dtouchrotate object

Rotate object after swipe according to swipe direction

Hi, I want my 2D object to rotate (look at) mouse/finger on the screen and after releasing input to start rotating.


I have the code which is doing what I need but in some cases it has a little bug that object starts to rotate in wrong direction(like bouncing of invisible wall).

  void LookAtMouse()
     {
         var ray = new Ray2D(transform.position, Input.mousePosition - transform.position);
         var extendedMousePosition = ray.GetPoint(300);
 
         float AngleRad = Mathf.Atan2(extendedMousePosition.y - transform.position.y, extendedMousePosition.x - transform.position.x);
         float AngleDeg = (180 / Mathf.PI) * AngleRad;
 
         var mousePos = Input.mousePosition;
         var newRotation = Quaternion.Euler(0, 0, AngleDeg - ObjectStartAngle - 1);
 
 
         this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg - ObjectStartAngle - 1);
         Debug.DrawLine(transform.position, mousePos, Color.red);
         Debug.DrawLine(transform.position, extendedMousePosition, Color.yellow);
     }


And here is my code to get swipe direction if user will release mouse button:

  void StartRotation()
     {
      
         var ray = new Ray2D(transform.position, Input.mousePosition - transform.position);
         var extendedMousePosition= ray.GetPoint(300);
 
         float AngleRad = Mathf.Atan2(extendedMousePosition.y - transform.position.y, extendedMousePosition.x - transform.position.x);
         // Get Angle in Degrees
         float AngleDeg = (180 / Mathf.PI) * AngleRad;
         // Rotate Object
         var newRoation = Quaternion.Euler(0, 0, AngleDeg - ObjectStartAngle - 1);
 
         swipeDirection = newRoation.eulerAngles - transform.rotation.eulerAngles;
 
         Debug.Log("Object:" + transform.rotation.eulerAngles);
         Debug.Log("Finger:" + newRoation.eulerAngles);
         Debug.Log("Swipe direction" + swipeDirection);
     }


And here is the problem. Sometimes especially during fast swipes or when user will stop movement at the end then swipeDirection is calculated incorrectly. I'm strugling with it for 3 days now and I find out that when the bug occur the object is ahead of mouse which seems to be strange..

Here are some gifs for better understanding. Correct behavior: alt text

Incorrect behavior: alt text

If there is any better way to check swipe direction? Or maybe do you know where is the issue in my code?

correct.gif (128.2 kB)
incorrect2.gif (166.3 kB)
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.

Follow this Question

Answers Answers and Comments

128 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

Related Questions

Rotate a 2D object slowly to the touch position 0 Answers

How can you make a 2D object move based on rotation, and rotate while moving? 1 Answer

How do I rotate a bullet (arrow) in Unity 2D? 0 Answers

Rotate player to LookAt a touch position 2 Answers

Bullet dissapears from screen when I use LookAt? 0 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