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
1
Question by Seidensticker · Apr 22, 2015 at 04:24 PM · 2d sprites2d rotation

Advanced rotation on 2D circle with mouse drag

Hello Unity, I need some advanced assistance to rotate a 2D circle object, no matter where the user starts to click/drag the object from. I will explain in as much detail as possible. Here is the 2D sprite image...alt text

The forward vector for the object is in the direction of the knob at the top of the image. When the user clicks and drags the top knob around, the object rotates correctly around the Z axis. But I want the user to be able to click and drag anywhere on the image to have it rotate around the Z axis.

I want it to simulate a steering wheel. If I grab the top of a steering wheel, and turn it clock wise, then the object should turn clock-wise, and then stay at that angle of rotation when i let go. If I then grab the bottom of the steering wheel and rotate it counter clock wise, then i want to rotate in the counter clock wise direction, and when i let go, stay in that position/rotation angle.

Currently, the object will quickly rotate to face the click-drag position of the mouse. For example, say I click and drag the top knob clock wise any amount of degrees, and then let go. The object will rotate correctly and stay in that position/rotation. If I then click on the opposite side of the circle, it will quickly rotate to face the click/drag position, and then continue to rotate as i drag the mouse around. I don't want it to jump-rotate, I want to be able to rotate it around the Z axis by clicking and dragging from any spot on the object, without it jumping into rotation. So, basically, If I click on the top of the object and drag in the right direction, I want the object to rotate clock wise around the Z Axis. If I click on the bottom of the object and drag in the right direction, I want the object to rotate counter clock wise around the Z axis, without any fast jump-rotation happening.

Here is the code I have right now....

 public void On_Drag(Gesture _Gesture)
 {
      if (_Gesture.pickedObject == gameObject)
      {
           SpinnerScreenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
           GesturePoint.x = _Gesture.position.x;
           GesturePoint.y = _Gesture.position.y;
           SpinnerScreenPoint = (GesturePoint - SpinnerScreenPoint);
           RotationAngle = (Mathf.Atan2(SpinnerScreenPoint.y, SpinnerScreenPoint.x) * Mathf.Rad2Deg - 90);
      }
 }
 
 void Update()
 {
      gameObject.transform.rotation = Quaternion.AngleAxis(RotationAngle, Vector3.forward);
 }



Please let me know if I need to better explain in more detail. Thank you for your help and assistance.

circleimage.png (24.5 kB)
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 Scorpion2018 · Mar 06, 2020 at 12:15 PM 0
Share

I think I have the same doubt as you, can you show a little more of the script because I don't understand how it works? Thanks in advance.

avatar image Seidensticker Scorpion2018 · Mar 06, 2020 at 01:31 PM 0
Share

Unfortunately I do not have the source code anymore.

1 Reply

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

Answer by JoeStrout · Apr 22, 2015 at 05:21 PM

You're very close. All you need to do is keep a bit more data. You need:

  • The original angle of the wheel (get this from the transform when the gesture begins)

  • The original angle at which the user tapped (compute this as you're doing now, when the gesture begins)

Now, when the gesture continues (e.g. in OnDrag), you compute the NEW angle of the gesture position. This tells you how much the transform should be rotated relative to its original position. For example:

 float newRot = Mathf.Atan2(SpinnerScreenPoint.y, SpinnerScreenPoint.x) * Mathf.Rad2Deg - 90;
 RotationAngle = OriginalRotAng + newRot - OriginalTouchAng;

where OriginalRotAng is the original rotation of the transform, and OriginalTouchAng is the initial angle of the touch, as described above.

Comment
Add comment · Show 3 · 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 Seidensticker · Apr 22, 2015 at 06:53 PM 1
Share

That's awesome, seriously! That is exactly the answer I needed. It works great now, just as I intended. You were right, I was very close, but i just need your guidance to complete this task. Cheers!

avatar image JoeStrout · Apr 22, 2015 at 06:58 PM 0
Share

Glad you got it working. Good luck with your project!

avatar image nik_2522 · Apr 20, 2016 at 05:19 AM 0
Share

Hi I am doing somewhat similar. But the only thing I want is to restrict backward rotation. Here is the link to my question:

link text

Can someone help me here :(

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet follows player mouvement when It instintiates. 1 Answer

help with freezing 2D sprite rotation 2 Answers

Gravity not working to right strength 0 Answers

Slow Sprite Rotation Toward Movement Direction - Top-Down 2D Game 0 Answers

Instantiate a GameObject with a specific Z rotation 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