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 /
  • Help Room /
avatar image
0
Question by yapaysinek · Sep 18, 2016 at 12:45 AM · rotationtouchquaternioneuleranglesrounding

If rotation was over 20° smoothly rotate towards the next 90°

Hey Unity Pros,

So basically what i want to achieve is I want my 3d cube to rotate towards its next 90° (0,90,180,270,360) if the touch rotation was greater than 20°. Now I've googled everything but nothing popped up that could solve my problem.however I did found out how to do this with 45°(with mathf.round) but that was way to much for me.

I'd really appreciate it if someone could help me with this because I've been struggling with this for quite some time now...

Thanks in advance!!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by brain56 · Sep 18, 2016 at 01:19 AM

Are you saying you want to round up the rotation to the nearest multiple of 90 if it's greater than 20? If that's the case, you can do something like:

 if(Number>20)
 {
     Number = (int) Math.Ceiling(Number / 90) * 90;
 }

If you want to perform the rotation smoothly, instead of snapping, you can look up interpolation, such as Mathf.Lerp. Alternatively, you can use third party tweening plugins such as DOTween which I highly recommend.

Comment
Add comment · 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
0

Answer by yapaysinek · Sep 18, 2016 at 05:37 PM

hey brain56,

(i had to post this as an answer because it was way too long for a comment thanks to the script -_-)

thanks for your answer! i have implemented your solution into my code and it works but also it doesnt at the same time. now if all the angles are 0 than rotation on the y and z axis are great but not on th x. also if you rotate eg: the y axis to 90 than the rotation gets all weird and rather than rotating on the axis i want it rotates to the direction i dont want to.. anyways.. here is my code so you can take a look at it and tell me where have i fxcked it up. oh and im working on mobile so the script its touch based.

thanks in advance!

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using DG.Tweening;
 
 public class rotate : MonoBehaviour {
 
     public Image debug_go;
     public Image hehi;
     public Image roth;
     public Image rotvl;
     public Image rotvr;
     public Text zeroed;
     public Text horSwipe;
     public Text horSwipeL;
 
     public float rotSpeed;
 
     public bool isRotatingHorizontal = false;
     public bool isRotatingVerticalL = false;
     public bool isRotatingVerticalR = false;
 
     public bool startPosDone = false;
 
     public float swipeDistHorizontal;
     public float swipeDistVertical;
 
     public GameObject targetItem;
 
     public float minSwipeDistX;
     public float minSwipeDistY;
     private Vector2 startPos;
     public Vector3 endPos;
 
     public float lerpspeed;
 
     float dist;
 
     public Vector3 startRot;
     bool once;
     bool touched;
 
     float angle;
     Vector2 startPos2nd;
     public bool heh = false;
 
     Vector3 currentangle;
 
     void Update () {
 
         if (targetItem.GetComponent<Rigidbody> ().IsSleeping ())
             debug_go.enabled = false;
         else
             debug_go.enabled = true;
         if (heh)
             hehi.enabled = true;
         else
             hehi.enabled = false;
         if (isRotatingHorizontal)
             roth.enabled = true;
         else
             roth.enabled = false;
         if (isRotatingVerticalL)
             rotvl.enabled = true;
         else
             rotvl.enabled = false;
         if (isRotatingVerticalR)
             rotvr.enabled = true;
         else
             rotvr.enabled = false;
         
         zeroed.text = targetItem.transform.eulerAngles.ToString ();
         horSwipeL.text = swipeDistHorizontal.ToString () + "x, " + swipeDistVertical.ToString () + "y";
         horSwipe.text = angle.ToString ();
 
         if (heh) {
 
             Vector3 vec = targetItem.transform.localEulerAngles;
 
             if (isRotatingHorizontal && !isRotatingVerticalL && !isRotatingVerticalR) {
 
                 if (endPos.x > 0) {
 
                     if (angle >= 100) {
                         vec.y = Mathf.Floor (vec.y / 90) * 90;
                     } 
                     else if (angle < 100) {
                         vec.y = Mathf.Ceil (vec.y / 90) * 90;
                     }
                 }
 
                 else if (endPos.x < 0f) {
 
                     if (angle >= 100) {
                         vec.y = Mathf.Floor (vec.y / 90) * 90;
                     } 
                     else if (angle < 100) {
                         vec.y = Mathf.Ceil (vec.y / 90) * 90;
                     }
                 }
 
                 vec.x = Mathf.Round(vec.x / 90) * 90;
                 vec.z = Mathf.Round(vec.z / 90) * 90;
             }
 
             else if (isRotatingVerticalL && !isRotatingVerticalR && !isRotatingHorizontal) {
 
                 if (endPos.y < 0f) {
                     
                     if (angle >= 100) {
                         vec.x = Mathf.Floor (vec.x / 90) * 90;
                     } 
                     else if (angle < 100) {
                         vec.x = Mathf.Ceil (vec.x / 90) * 90;
                     }
                 }
 
                 else if (endPos.y > 0f) {
                     
                     if (angle >= 100) {
                         vec.x = Mathf.Floor (vec.x / 90) * 90;
                     } 
                     else if (angle < 100) {
                         vec.x = Mathf.Ceil (vec.x / 90) * 90;
                     }
                 }
         
                 vec.z = Mathf.Round(vec.z / 90) * 90;
                 vec.y = Mathf.Round(vec.y / 90) * 90;
             }
 
             else if (isRotatingVerticalR && !isRotatingVerticalL && !isRotatingHorizontal) {
 
                 if (endPos.y < 0f) {
 
                     if (angle >= 100) {
                         vec.z = Mathf.Floor (vec.z / 90) * 90;
                     } 
                     else if (angle < 100) {
                         vec.z = Mathf.Ceil (vec.z / 90) * 90;
                     }
                 }
 
                 else if (endPos.y > 0f) {
 
                     if (angle >= 100) {
                         vec.z = Mathf.Floor (vec.z / 90) * 90;
                     } 
                     else if (angle < 100) {
                         vec.z = Mathf.Ceil (vec.z / 90) * 90;
                     }
                 }
                 vec.x = Mathf.Round(vec.x / 90) * 90;
                 vec.y = Mathf.Round(vec.y / 90) * 90;
             }
 
             //targetItem.transform.localEulerAngles = Vector3. (targetItem.transform.localEulerAngles, vec, rotSpeed * Time.deltaTime);
             targetItem.transform.DORotate (vec, rotSpeed * Time.deltaTime, RotateMode.Fast);
 
             if (isRotatingHorizontal) {
 
                 if (targetItem.transform.localEulerAngles.y < 0.01f && targetItem.transform.localEulerAngles.y > -0.01f ||
                     targetItem.transform.localEulerAngles.y < 90.01f && targetItem.transform.localEulerAngles.y > 89.99f ||
                     targetItem.transform.localEulerAngles.y < 180.01f && targetItem.transform.localEulerAngles.y > 179.99f ||
                     targetItem.transform.localEulerAngles.y < 270.01f && targetItem.transform.localEulerAngles.y > 269.99f ||
                     targetItem.transform.localEulerAngles.y < 360.01f && targetItem.transform.localEulerAngles.y > 359.99f ||
                     targetItem.transform.localEulerAngles.y < 0.01f && targetItem.transform.localEulerAngles.y > -0.01f ||
                     targetItem.transform.localEulerAngles.y > -90.01f && targetItem.transform.localEulerAngles.y < -89.99f ||
                     targetItem.transform.localEulerAngles.y > -180.01f && targetItem.transform.localEulerAngles.y < -179.99f ||
                     targetItem.transform.localEulerAngles.y > -270.01f && targetItem.transform.localEulerAngles.y < -269.99f ||
                     targetItem.transform.localEulerAngles.y > -360.01f && targetItem.transform.localEulerAngles.y < -359.99f) { //this is to correct floating points and snap
 
                     heh = false;
                     isRotatingHorizontal = false;
                     isRotatingVerticalR = false;
                     isRotatingVerticalL = false;
                     startPosDone = false;
                     once = false;
                 }
             }
             else if (isRotatingVerticalL) {
 
                 if (targetItem.transform.localEulerAngles.x < 0.01f && targetItem.transform.localEulerAngles.x > -0.01f ||
                     targetItem.transform.localEulerAngles.x < 90.01f && targetItem.transform.localEulerAngles.x > 89.99f ||
                     targetItem.transform.localEulerAngles.x < 180.01f && targetItem.transform.localEulerAngles.x > 179.99f ||
                     targetItem.transform.localEulerAngles.x < 270.01f && targetItem.transform.localEulerAngles.x > 269.99f ||
                     targetItem.transform.localEulerAngles.x < 360.01f && targetItem.transform.localEulerAngles.x > 359.99f ||
                     targetItem.transform.localEulerAngles.x < 0.01f && targetItem.transform.localEulerAngles.x > -0.01f ||
                     targetItem.transform.localEulerAngles.x > -90.01f && targetItem.transform.localEulerAngles.x < -89.99f ||
                     targetItem.transform.localEulerAngles.x > -180.01f && targetItem.transform.localEulerAngles.x < -179.99f ||
                     targetItem.transform.localEulerAngles.x > -270.01f && targetItem.transform.localEulerAngles.x < -269.99f ||
                     targetItem.transform.localEulerAngles.x > -360.01f && targetItem.transform.localEulerAngles.x < -359.99f) { //this is to correct floating points and snap
 
                     heh = false;
                     isRotatingHorizontal = false;
                     isRotatingVerticalR = false;
                     isRotatingVerticalL = false;
                     startPosDone = false;
                     once = false;
                 }
             }
             else if (isRotatingVerticalR) {
 
                 if (targetItem.transform.localEulerAngles.z < 0.01f && targetItem.transform.localEulerAngles.z > -0.01f ||
                     targetItem.transform.localEulerAngles.z < 90.01f && targetItem.transform.localEulerAngles.z > 89.99f ||
                     targetItem.transform.localEulerAngles.z < 180.01f && targetItem.transform.localEulerAngles.z > 179.99f ||
                     targetItem.transform.localEulerAngles.z < 270.01f && targetItem.transform.localEulerAngles.z > 269.99f ||
                     targetItem.transform.localEulerAngles.z < 360.01f && targetItem.transform.localEulerAngles.z > 359.99f ||
                     targetItem.transform.localEulerAngles.z < 0.01f && targetItem.transform.localEulerAngles.z > -0.01f ||
                     targetItem.transform.localEulerAngles.z > -90.01f && targetItem.transform.localEulerAngles.z < -89.99f ||
                     targetItem.transform.localEulerAngles.z > -180.01f && targetItem.transform.localEulerAngles.z < -179.99f ||
                     targetItem.transform.localEulerAngles.z > -270.01f && targetItem.transform.localEulerAngles.z < -269.99f ||
                     targetItem.transform.localEulerAngles.z > -360.01f && targetItem.transform.localEulerAngles.z < -359.99f) { //this is to correct floating points and snap
 
                     heh = false;
                     isRotatingHorizontal = false;
                     isRotatingVerticalR = false;
                     isRotatingVerticalL = false;
                     startPosDone = false;
                     once = false;
                 }
             }
         }
 
         if (Input.touchCount > 0) {
 
             Touch touch = Input.touches [0];
 
             switch (touch.phase) {
 
             case TouchPhase.Began:
 
                 startPos = touch.position;
                 startPos2nd = touch.position;
 
                 angle = 0f;
 
                 if (heh == false) {
                     isRotatingHorizontal = false;
                     isRotatingVerticalR = false;
                     isRotatingVerticalL = false;
                     startPosDone = false;
                 }
                 break;
 
             case TouchPhase.Moved:
                 
                 if (!startPosDone && !heh) {
 
                     swipeDistHorizontal = Vector3.Distance (new Vector3 (startPos.x, 0, 0), new Vector3 (touch.position.x, 0, 0));
                     swipeDistVertical = Vector3.Distance (new Vector3 (0, startPos.y, 0), new Vector3 (0, touch.position.y, 0));
                 }
 
                 if (swipeDistHorizontal > minSwipeDistX && !isRotatingVerticalR && !isRotatingVerticalL) {
 
                     if (startPosDone && !once) {
                         startPos2nd.x -= swipeDistHorizontal;
                         once = true;
                     }
                     targetItem.transform.Rotate (0, -(touch.deltaPosition.x / 1.5f), 0, Space.Self);
                     angle = Vector3.Distance (new Vector3 (startPos2nd.x, 0, 0), new Vector3 (touch.position.x, 0, 0));
                     isRotatingHorizontal = true;
                     startPosDone = true;
                 } 
 
                 else if (swipeDistVertical > minSwipeDistY && isRotatingHorizontal == false) {
 
                     if (startPos.x < Screen.width / 2 && !isRotatingVerticalR) {
 
 
                         if (startPosDone && !once) {
                             startPos2nd.y -= swipeDistVertical;
                             once = true;
                         }
 
                         targetItem.transform.Rotate ((touch.deltaPosition.y / 1.5f), 0, 0, Space.Self);
                         angle = Vector3.Distance (new Vector3 (0, startPos2nd.y, 0), new Vector3 (0, touch.position.y, 0));
                         isRotatingHorizontal = false;
                         isRotatingVerticalL = true;
                         isRotatingVerticalR = false;
                         startPosDone = true;
                     }
 
                     else if (startPos.x > Screen.width / 2 && !isRotatingVerticalL) {
 
                         if (startPosDone && !once) {
                             startPos2nd.y -= swipeDistVertical;
                             once = true;
                         }
 
                         targetItem.transform.Rotate (0, 0, (touch.deltaPosition.y / 1.5f), Space.Self);
                         angle = Vector3.Distance (new Vector3 (0, startPos2nd.y, 0), new Vector3 (0, touch.position.y, 0));
                         isRotatingHorizontal = false;
                         isRotatingVerticalL = false;
                         isRotatingVerticalR = true;
                         startPosDone = true;
                     }
                 }
                 break;
 
             case TouchPhase.Ended:
 
                 if (swipeDistHorizontal > minSwipeDistX || swipeDistVertical > minSwipeDistY) {
                     heh = true;
                     endPos = startPos - touch.position;
                 }
                 break;
             }
         }
     }
 }
 



Comment
Add comment · 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

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

95 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

Related Questions

How to make a 2D sprite rotate towards a specific point while facing perpendicular to the camera ? 1 Answer

Quaternion Rotation - AngleAxis/EulerAngle 2 Answers

Quaternions: Setting rotation of one gameObject to Y,Z-Axis rotation of gameObject-A and Z-Axis position of gameObject-B 0 Answers

How to add to a rotation angle? 0 Answers

Quaternion Rotation Issue 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