1
\$\begingroup\$

I've been working on a project where I have a cube. When swiped left, the cube will rotate 90 degrees the left, and then when swiped right, the cube will rotate 90 degrees to the right. I have this area working fine.

However, when i go to swipe up and down (To rotate the cube 90 forwards) it becomes slightly unstuck. If the rotation of the object is (0, 0, 0), it works fine. But if I, for example, rotate once to the left and then tried rotate fowards, the cube will correctly rotate left, but then the forward rotation will rotate on the Z axis and just spin the cube (So the face looks like its moving in right in a clockwise direction).

Here is the code:

public Transform rotateAround;
public float rotateSpeed;

bool isRotating;
Touch touchOn;
Touch touchEnd;

float changeX, changeY;

// Update is called once per frame
void Update()
{
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        touchOn = Input.GetTouch(0);
    }   

    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
    {
        touchEnd = Input.GetTouch(0);
        var difference = touchOn.deltaPosition - touchEnd.deltaPosition;

        changeX = (touchOn.position.x - touchEnd.position.x);
        changeY = (touchOn.position.y - touchEnd.position.y);

        Debug.Log("Changes: x: " + changeX + " y: " + changeY);

        if (changeX > 25f)
        {
            StartCoroutine(Rotate(transform, rotateAround, -Vector3.up, 90, rotateSpeed));
        }
        else if (changeX < -25f)
        {
            StartCoroutine(Rotate(transform, rotateAround, Vector3.up, 90, rotateSpeed));
        }

        if (changeY > 25f)
        {
            StartCoroutine(Rotate(transform, rotateAround, Vector3.left, 90, rotateSpeed));
        }
        else if (changeY < -25)
        {
            StartCoroutine(Rotate(transform, rotateAround, -Vector3.left, 90, rotateSpeed));
        }
    }
}

public IEnumerator Rotate(Transform thisTransform, Transform otherTransform, Vector3 rotateAxis, float degrees, float totalTime)
{
    if (!isRotating)
    {
        isRotating = true;

        Quaternion startRotation = thisTransform.localRotation;
        Vector3 startPosition = thisTransform.position;

        transform.RotateAround(otherTransform.position, rotateAxis, degrees);

        Quaternion endRotation = thisTransform.rotation;
        Vector3 endPosition = thisTransform.position;

        thisTransform.rotation = startRotation;
        thisTransform.position = startPosition;

        float rate = degrees / totalTime;

        for (float i = 0.0f; i < degrees; i += Time.deltaTime * rate)
        {
            yield return null;
            thisTransform.RotateAround(otherTransform.position, rotateAxis, Time.deltaTime * rate);
        }

        thisTransform.rotation = endRotation;
        thisTransform.position = endPosition;
        isRotating = false;
    }
}

Both the camera and the cube start with rotations of 0, 0, 0. The script is placed on the camera, with the rotateAround transform object been set as a cube.

Any help would be appreciated!

\$\endgroup\$
1
\$\begingroup\$

You describe your problem rotating a cube, but it seems to me that you are having the camera orbit around the cube. However, the vectors you are using for the axes of rotation are in world space, and do not rotate with the camera. After you have rotated left by 90 degrees, Vector3.left is now facing directly into the camera. As such, your next swipe for a "forward" rotation just causes the camera to spin in place.

If you want a fixed background, where the cube is the only thing that moves, you will have to rework the code to rotate the cube with respect to a fixed camera.

If you want the camera to move around the cube as well as the background and everything else, then you should use axes of rotation that are relative to the camera. Instead of Vector3.forward, Vector3.left, and Vector3.up, you should use transform.forward, transform.right, and transform.up (with appropriate signs for the two directions of rotation).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.