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 karlkerschl · Jun 19, 2016 at 03:57 PM · scripting problemclampquaternionsclamped rotation

Clamping Wrapping Rotation to Fixed Angle

Hi all! I know this has been asked before but I'm just learning and I can't seem to apply any of the answers to my specific problem.

I'm trying to rotate a cube via touch and drag and I'd like it to snap to a 90 degree rotation on the Y axis upon touch release. I want the cube to rotate left or right as the user is dragging (so they can peek at either side before making a decision about the rotation direction they want) and then snap to whichever 90 degree rotation is the closest. I've got all of this working by referencing a separate cube offscreen which only rotates to 90 degrees.

The hitch is: When the user is dragging and rotating the cube, I want to clamp the rotation so that they can only ever see one adjacent side of the cube, not be able to just keep rotating it as long as they drag (ie. only ever rotate to + or - 90 degrees while dragging and then snap to the nearest 90 degrees upon release). I've tried a bunch of things but I don't know how to deal with the wrapping euler rotation. If they drag left and the rotation wraps from 0 to 360 then what are my clamping parameters? I can't use negative euler values, right? Any pointer in the right direction would be appreciated! Thanks!!

Here's my code:

     void OnMouseDrag (){
 
         if(dragging)
         {
             // offset
             touchOffset = (Input.mousePosition - touchLocation);
 
             // "rotate" angle.y:
             angle.y += -(touchOffset.x + touchOffset.y) * sensitivity;
 
             // clamp it to the desired limits:
             //angle.y = Mathf.Clamp(angle.y, 0, 90); //don't know what these clamp parameters should be!!
 
             // update rotation:
             transform.eulerAngles = angle;
 
             // store mouse
             touchLocation = Input.mousePosition;
         }
     }
 
     void OnMouseUp() {
 
         dragging = false;
 
         endingVec = transform.eulerAngles;
         endingVec.y = Mathf.Round(endingVec.y / 90) * 90; //Round to nearest 90 degrees
 
         angle = endingVec; //reset clamped angle here??
 
 
         //turn refCube hard 90 degrees (this is probably overcomplicated and busted)
 
         if (startingVec.y == 0 && endingVec.y == 90 || startingVec.y == 360 && endingVec.y == 90) {
             QuarterTurnLeft();
         }
         if (startingVec.y == 90 && endingVec.y == 180) {
             QuarterTurnLeft();
         }
         if (startingVec.y == 180 && endingVec.y == 270) {
             QuarterTurnLeft();
         }
         if (startingVec.y == 270 && endingVec.y == 0 || startingVec.y == 270 && endingVec.y == 360) {
             QuarterTurnLeft();
         }
         if (startingVec.y == 0 && endingVec.y == 270) {
             QuarterTurnRight();
         }
         if (startingVec.y == 270 && endingVec.y == 180) {
             QuarterTurnRight();
         }
         if (startingVec.y == 180 && endingVec.y == 90) {
             QuarterTurnRight();
         }
         if (startingVec.y == 90 && endingVec.y == 0) {
             QuarterTurnRight();
         }
 
     }
 
     void QuarterTurnLeft() {
         from = refCube.transform;
         refCube.transform.Rotate(0, 90, 0);
         to = refCube.transform;
     }
 
     void QuarterTurnRight() {
         from = refCube.transform;
         refCube.transform.Rotate(0, -90, 0);
         to = refCube.transform;
 
     }

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

57 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

Related Questions

Object don't rotate correctly 1 Answer

Clamping a camera with weird results 0 Answers

Camera clamps to the world scale, not local :c 1 Answer

What is wrong with clamping? 1 Answer

MathF clamp look toward mouse pointer not working correctly 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