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 $$anonymous$$ · Apr 09, 2018 at 10:21 AM · touchrotate objectswiperotatearoundpivot

Rotate camera based on left/right swipe

Hi!

I'm trying to rotate a parent object as a pivot point with my camera as the child. I want to change the rotation y-value of the parent object based on the swipe distance. The problem is that the object rotates too fast and makes it look like it's jumping on the screen. Also when I swipe left or right it's not going to the right direction. I'm not sure what's wrong other then my distanceMag variable is too big. And is this the correct way to solve this problem? Any help or tips would be appreciated. Thanks!

 if (Input.touchCount > 0)
         {
             foreach (var touch in Input.touches)
             {
                 switch (touch.phase)
                 {
                     case TouchPhase.Began:
                         prevFingerPos = touch.position; break;
                     case TouchPhase.Moved:
                         if (touch.fingerId == 0)
                         {
                             currentFingerPos = touch.position;
                             touchDistance = Vector2.Distance(prevFingerPos, currentFingerPos);
                             float timePassed = Time.deltaTime;
                             float touchSpeed = touchDistance * timePassed;
                             newDistance = Vector2.Lerp(prevFingerPos, currentFingerPos, touchSpeed);
                             distanceMag = newDistance.magnitude;
                             //TestRotation(distanceMag);
                             Rotate(distanceMag);
                         }
                         break;
                     case TouchPhase.Stationary:
                         prevFingerPos = currentFingerPos; break;
                     case TouchPhase.Ended:
                         touchDistance = 0;
                         prevFingerPos = currentFingerPos = new Vector2();
                         break;
                 }
             }
         }
 void Rotate(float distance)
     {
         Vector3 tempLocalRotation = transform.localRotation.eulerAngles;
         float rotateY;
         if (distance < 0) { rotateY = tempLocalRotation.y - distance; }
         else if (distance > 0) { rotateY = tempLocalRotation.y + distance; }
         else { rotateY = tempLocalRotation.y; }
         float min = -360, max = 360;
         rotateY = Mathf.Clamp(rotateY, min, max);
         rotateAroundYAxis = new Vector3(0, rotateY, 0);
         transform.localEulerAngles = rotateAroundYAxis;
     }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Harinezumi · Apr 09, 2018 at 12:19 PM

I haven't used touch controls before, but I think you should update prevFingerPos at the end of TouchPhase.Moved as well, or the distance will become incrementally bigger, causing non-linear rotation in relation to the distance moved.
Also, I'm not sure what you want to express with the part where you calculate touchSpeed (what is distance multiplied by time?) and newDistance... you could just use touchDistance.magnitude for the rotation.
Next, it is rotating in the wrong direction, because you subtract when distance is negative, which is addition, counter-clockwise rotation, then add it when it is positive, which is again addition. Instead, just add it as it is, no need to check its sign.

Comment
Add comment · Show 7 · 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 $$anonymous$$ · Apr 10, 2018 at 08:34 AM 0
Share

Thanks Harizumi for the tips. I've added prevFingerPos add the end of TouchPhase.$$anonymous$$oved as well. I wanted to know the swipe speed so ins$$anonymous$$d of touchSpeed, I should call it swipeSpeed. Also I've altered my code and I'm no longer using my own Rotate function anymore. Ins$$anonymous$$d I'm using the Rotate function provided by Unity and rotate to the direction based on the left and right swipe. This works but has a problem. When I stop swiping, the object keeps rotating for a bit more until it stops. And also it lags when I switch direction. Is there any way to counter this? Perhaps putting a check within the TouchPhase.$$anonymous$$oved to check if swipe has stopped? That would be a bad solution I think..

avatar image Harinezumi $$anonymous$$ · Apr 10, 2018 at 08:40 AM 0
Share

Can you show some code? It is difficult to deter$$anonymous$$e from textual description what could be the problem.

avatar image $$anonymous$$ Harinezumi · Apr 10, 2018 at 08:52 AM 0
Share

Can't seem to add the code properly :/ switch (touch.phase) { case TouchPhase.Began: prevFingerPos = touch.position; break; case TouchPhase.$$anonymous$$oved: if (touch.fingerId == 0) { currentFingerPos = touch.position; swipeDistance = Vector2.Distance(prevFingerPos, currentFingerPos); float swipeSpeed = swipeDistance / Time.deltaTime; newDistance = Vector2.Lerp(prevFingerPos, currentFingerPos, swipeSpeed); float rotation = newDistance.magnitude Time.deltaTime inputSensitivity; //swipe right if (prevFingerPos.x < currentFingerPos.x){ transform.Rotate(Vector3.up, rotation); //swipe left }else if (prevFingerPos.x > currentFingerPos.x){ transform.Rotate(Vector3.down, rotation); } if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Ended) { // smooth rotation here or in the switch case } prevFingerPos = touch.position; } break; case TouchPhase.Stationary: prevFingerPos = touch.position; break; case TouchPhase.Ended: prevFingerPos = currentFingerPos = new Vector2(); break; }

Show more comments

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

99 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

Related Questions

TouchPhase doesn't end when player swipes off screen 1 Answer

mouse input to touch input help please 0 Answers

Swipe menu, problem! 0 Answers

Deactivate Swipe Controls on Pause? 0 Answers

How to get intermediate swipe positions? 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