0

I'm working on a simple puzzle-style Unity game for iOS. The game presents the user a "rubiks cube" object (one large cube comprised of smaller cubes).

I want users to be able to swipe left/right/up/down on the cube to rotate as expected.

My code works, but after the user performs a 2nd rotation, the cube does not rotate according to the user's expected direction. After the first rotation, the x/y/z axis have rotated along with the device.

If I only use the left/right swipe code, it works as expected. When I add up/down swiping, it breaks. Which makes sense, since the axis doesn't rotate when I stick to two variables.

Here is my code:

if (Input.GetMouseButtonUp(0))
    {

        //swipe upwards
        if (currentSwipe.y > 0 && currentSwipe.x > -1f && currentSwipe.x < 1f)
        {
            GameObject.FindGameObjectWithTag("Sfx").GetComponent<SoundManager>().PlaySwipe();

            gameObject.transform.DORotateQuaternion(Quaternion.Euler(0f, 0f, 90f), 0.5f).SetRelative(true).OnComplete(SetSwiping);
        }
        //swipe down
        if (currentSwipe.y < 0 && currentSwipe.x > -1f && currentSwipe.x < 1f)
        {
            GameObject.FindGameObjectWithTag("Sfx").GetComponent<SoundManager>().PlaySwipe();

            gameObject.transform.DORotateQuaternion(Quaternion.Euler(0f, 0f, -90f), 0.5f).SetRelative(true).OnComplete(SetSwiping);
        }
        //swipe left
        if (currentSwipe.x < 0 && currentSwipe.y > -1f && currentSwipe.y < 1f)
        {
            GameObject.FindGameObjectWithTag("Sfx").GetComponent<SoundManager>().PlaySwipe();

            gameObject.transform.DORotateQuaternion(Quaternion.Euler(0f, -90f, 0f), 0.5f).SetRelative(true).OnComplete(SetSwiping);
        }
        //swipe right
        if (currentSwipe.x > 0 && currentSwipe.y > -1f && currentSwipe.y < 1f)
        {
            GameObject.FindGameObjectWithTag("Sfx").GetComponent<SoundManager>().PlaySwipe();

            gameObject.transform.DORotateQuaternion(Quaternion.Euler(0f, 90f, 0f), 0.5f).SetRelative(true).OnComplete(SetSwiping);

        }
    }

Any help is appreciated! I'm relatively new to Unity so still learning.

2
  • The first time that I see DORotateQuaternion. Where did you get that one from? Perhaps you could add a few pictures so we could understand your problem better. Commented Oct 24, 2022 at 11:51
  • @Voidsay it's from DOTween: dotween.demigiant.com and I know Demigiant docs say to use DORotate first, but my app is hitting the niche case where DORotateQuaternion is needed Commented Oct 24, 2022 at 16:20

1 Answer 1

1

I think it is easier (at least for me) to rethink the setup like this:

private Quaternion _targetRotation; // this will jump to the rotation in discrete steps (i.e. not gradually)

Start(){
    _targetRotation = cube.transform.localRotation;
    ....
}
...
    var appliedRotation = Quaterion.identity;

    //swipe upwards
    if (currentSwipe.y > 0 && currentSwipe.x > -1f && currentSwipe.x < 1f){
        appliedRotation = Quaternion.AngleAxis(90f, Vector3.right);
    }
    ... // other swipe cases

    _targetRotation = appliedRotation * _targetRotation; // order is important afaik
    
    ...
    gameObject.transform.DOLocalRotateQuaternion(_targetRotation);

let me know if this works.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.