- Home /
Rotating the cube with Quaternion.Lerp
Hi. I'm working on the rotation of the rubik's cube and having some problems. The first rotation always rotates correctly(90 degrees). However, the rotations coming after the first one are not working correctly; rotates 180 degrees when I try to rotate the cube opposite way as the first rotate, or rotates on two axis when I try to rotate the cube in a different axis as the first rotate.
After the mouse click, I determine the rotation of the cube with FindRotationOfCube(). Then I calculate the direction of the rotation depends on the Mouse movement in RotateDirection(). Last, I rotate the cube with Quaternion.Lerp in RotateSlice().
Here's my code.
private void Update()
{
switch(stateRotate)
{
case "WaitForMouseInput":
WaitForMouseInput();
break;
case "FindRotationOfCube":
FindRotationOfCube();
break;
case "RotateDirection":
RotateDirection();
break;
case "RotateSlice":
RotateSlice();
break;
}
}
void WaitForMouseInput()
{
if(mScript.canRotate)
{
stateRotate = "FindRotationOfCube";
}
}
void FindRotationOfCube()
{
fromRotation = cubie5.transform.rotation;
rotX = cubie5.transform.rotation.x;
rotY = cubie5.transform.rotation.y;
rotZ = cubie5.transform.rotation.z;
stateRotate = "RotateDirection";
}
void RotateDirection()
{
if(mScript.mouseMovedDir == "Left")
{
toRotation = Quaternion.Euler(rotX, rotY + 90, rotZ);
}
else if(mScript.mouseMovedDir == "Right")
{
toRotation = Quaternion.Euler(rotX, rotY-90, rotZ);
}
else if (mScript.mouseMovedDir == "Up")
{
toRotation = Quaternion.Euler(rotX, rotY, rotZ - 90);
}
else if (mScript.mouseMovedDir == "Down")
{
toRotation = Quaternion.Euler(rotX, rotY, rotZ + 90);
}
stateRotate = "RotateSlice";
}
void RotateSlice()
{
cubie5.transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * 10);
stateRotate = "WaitForMouseInput";
}
Have any suggestions? Thanks.
It will be helpful if you can post more codes, and perhaps provide us with screenshots and what are the steps done to produce the result in the screenshots.
At least for me, I am having trouble trying to visualize it.
Are you updating rotX/rotY/rotZ before setting toRotation?
Answer by robertbu · Mar 03, 2013 at 08:22 AM
Here's a solution. A bit ugly, and I am a bit frustrated that I could not figure out the qTo rotation using Quaternion methods rather than RotateAround(). You can replace the RotateTowards() with Lerp() (since that was your question) if you like, but I liked the feel of RotateTowards() better:
public class RotationsBy90 : MonoBehaviour {
public float speed = 55.0f;
private Quaternion qTo = Quaternion.identity;
private float treshold = 1.0f;
void Update () {
if (Quaternion.Angle (transform.rotation, qTo) > treshold) {
transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, Time.deltaTime * speed);
}
else {
transform.rotation = qTo;
Quaternion qSave;
if (Input.GetKeyDown (KeyCode.UpArrow)) {
qSave = transform.rotation;
transform.RotateAround (transform.position, Vector3.right, 90.0f);
qTo = transform.rotation;
transform.rotation = qSave;
}
else if (Input.GetKeyDown (KeyCode.DownArrow)) {
qSave = transform.rotation;
transform.RotateAround (transform.position, Vector3.left, 90.0f);
qTo = transform.rotation;
transform.rotation = qSave;
}
else if (Input.GetKeyDown(KeyCode.LeftArrow)) {
qSave = transform.rotation;
transform.RotateAround (transform.position, Vector3.up, 90.0f);
qTo = transform.rotation;
transform.rotation = qSave;
}
else if (Input.GetKeyDown (KeyCode.RightArrow)) {
qSave = transform.rotation;
transform.RotateAround (transform.position, Vector3.down, 90.0f);
qTo = transform.rotation;
transform.rotation = qSave;
}
}
}
}
Thanks for the solution. It works perfectly. Also, using RotateTowards() looks better than using Lerp(). I couldn't understand the logic about using qSave. Could you explain the code after we get input from keyboard?
transform.RotateAround() is a rotation relative to the current rotation, but it happens immediately. So what I'm doing is saving the current rotation, doing an immediate (but relative) 90 degree rotation to generate the rotation that I want to use as qTo, and then restoring the original rotation. If I did not save and restore the rotation, you would see an immediate rotation rather than a rotation over time. I really don't like doing it way, but by quick try with using the Quaternion class directly failed. Quaternion methods are all absolute rotations rather than relative rotations.