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 Bartol0ll0 · Mar 16, 2016 at 02:06 PM · anglescircletransform.rotatetangents

Relative rotation with touch sometimes jumps to new rotation

Hello,I have circle in my game that i use to rotate while draging my finger on the screen.When i take finger off the screen,and then drag from another place,the circle should continue rotating from previous rotation instead of instantly changing rotation.It kinda works but my problem is that sometimes - I can't tell when that happens exactly,the circle jumps to new rotation instead of continuation.Can someone tell me where i have error in my script?

Here's the exact part of the script :

 if (Input.touchCount > 0)
         {
 
             Vector3 pos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
             Vector2 touchPos = new Vector2(pos.x, pos.y);
             if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 startAngle = Mathf.Atan2(touchPos.y,touchPos.x) * Mathf.Rad2Deg;
                 if (startAngle <= 0)
                 {
                     startAngle += 360;
                 }
             }
             angle = Mathf.Atan2(touchPos.y, touchPos.x) * Mathf.Rad2Deg;
             if(angle < 0)
                 angle += 360;
             distanceAngle = startAngle - endAngle;
             angle -= distanceAngle;
             if (angle < 0)
                 angle += 360;
             else if (angle > 360)
                 angle -= 360;
             if (Input.GetTouch(0).phase == TouchPhase.Ended)
             {
                 endAngle = angle;
             }
             
             circle.rotation = Quaternion.Euler(0f, 0f,angle);

 
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 digzou · Mar 16, 2016 at 02:05 PM 0
Share

I also kinda bumped into a similar situation. Did not go through your code but try adding this before checking touch ie before line 1 :

if (Input.Get$$anonymous$$ouseButtonDown (0)) return;

You simply ignore the first touch which rotates it to a weird angle.

avatar image Bartol0ll0 digzou · Mar 16, 2016 at 08:51 PM 0
Share

I'm afraid this doesn't work,it makes the rotation jump all the time :(

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eno-Khaon · Mar 17, 2016 at 12:04 AM

Let's break this down quickly:

Your object current has no rotation, so zero degrees.

You press your finger down at 45 degrees and circle around to 90 degrees. Your starting rotation is now 45 and your ending rotation is now 90.

You press your finger down at 180 and rotate around to 270. Your new starting angle is 180 and your ending angle changes from 45 to 135.

Going back to the start, that means you cancel out the difference between your finger's angle and your starting angle 45 - 45 = 0. Then, when you release your finger, the final rotation is 45 degrees, based around endAngle = angle - startAngle.

For the second set, you're now updating the starting angle. 180 - 180 = 0 The object is currently at 45 degrees, therefore your rotational sum comes from current - start + end or, in this case, 180 - 180 + 45. Rotate it around and you now have 270 - 180 + 45 for a 135 degree rotation.

With all the logic in mind, you almost had it right. A single oversight resulted in the following line:

 endAngle = angle;

Your ending angle is not accommodating any starting angle aside from 0 degrees. Instead, it should read:

 endAngle = angle - startAngle + endAngle;
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 Bartol0ll0 · Mar 17, 2016 at 12:16 PM 0
Share

If you are saying that i have to change endAngle = angle to endAngle = angle - startAngle in my "Ended" phase,this makes the rotation even more messy,it never start rotation from the good angle.I think my endAngle part is good,because it just remembers the last angle i had my finger on.Or just i can't exactly understand what do you mean.Can you please show the whole code with $$anonymous$$e things changed that you say are wrong?

avatar image Eno-Khaon Bartol0ll0 · Mar 23, 2016 at 08:52 PM 0
Share

Ack! I only just found this reply -- Sorry for my late response.

A general overview of the code would look something like this:

 // On initial touch
 startingAngle = $$anonymous$$athf.Atan2(touchPos.y,touchPos.x);
 
 // During active touch and motion
 touchAngle = $$anonymous$$athf.Atan2(touchPos.y, touchPos.x);
 float difference = (touchAngle - startingAngle) * $$anonymous$$athf.Rad2Deg;
 float currentTotal = (touchAngle - startingAngle + endingAngle) * $$anonymous$$athf.Rad2Deg;
 
 // Touch ends, finger lifted
 endingAngle = touchAngle - startingAngle + endingAngle;
 float finalTotal = endingAngle * $$anonymous$$athf.Rad2Deg;

Realizing now that I also forgot to add endAngle to its own value, I've edited the main reply to match.

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

Make a navmesh object start moving in a circle around an object 0 Answers

Transform.eulerAngles only x and z 2 Answers

Rotation around z-axis always local space and not world? Vector-axis angle projection calcuations. 0 Answers

Creating circles using script 1 Answer

2D physics object acts weird when pressed into other physics object 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