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;
}
Your answer
Follow this Question
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