#I just want to rotate a cube!

1 messages · Page 1 of 1 (latest)

twilit moat
#

Hi! So I'm a complete beginner to programming and Unity. I am a artist who just wants to bring their game making dreams to life. I've always heard to start off with a very simple game to learn, so I came up with this idea for a cube swipe game. However I can't even get the cube to rotate properly! It makes me feel so defeated.

** I am using the latest version of Unity. The game I'm making is viewing the scene directly straight, making the game appear 2D. However, the cube itself is 3D. The idea is that when you press A, the cube will rotate 0, -90 , 0. Then when you press D it will rotate 0, +90 , 0. When you press W it will rotate +90 , 0 , 0 , and when you press S it will rotate -90 , 0 , 0. The player should be able to turn the cube 90 degrees whenever they want. So left, right, up, down. Also I want the cube to rotate smoothly rather than instantly turn. I will eventually translate W,A,S,D to just tapping on left, right, up, down on the screen to make the game a mobile game.

the issue I deal with is that sometimes the cube will just turn incorrectly! Say the yellow side is facing the camera. If I press W, it will rotate clockwise rather than rotate up to the next side. which I think is due to unity refusing to go past 90 degrees and looping back to 0. Its very frustrating and i cant find any tutorials to properly execute my idea!

Please help! I'm so close to giving up and doing something else or even going to another engine!!

viscid shard
#

You need to show the code that rotates the cube. 👇

#

!code

queen terraceBOT
twilit moat
#

gotcha! I'll do it in a bit!

twilit moat
#
    public GameObject objectToRotate;
    Quaternion targetRotation;
    public float rotationSpeed = 5f;

    private void Update()
    {
        CheckRotation();
    }

    void CheckRotation()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            targetRotation = Quaternion.Euler(objectToRotate.transform.eulerAngles.x, objectToRotate.transform.eulerAngles.y + 90, objectToRotate.transform.eulerAngles.z);
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            targetRotation = Quaternion.Euler(objectToRotate.transform.eulerAngles.x, objectToRotate.transform.eulerAngles.y - 90, objectToRotate.transform.eulerAngles.z);
        }
        if (Input.GetKeyDown(KeyCode.W))
        {
            targetRotation = Quaternion.Euler(objectToRotate.transform.eulerAngles.x + 90, objectToRotate.transform.eulerAngles.y, objectToRotate.transform.eulerAngles.z);
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            targetRotation = Quaternion.Euler(objectToRotate.transform.eulerAngles.x - 90, objectToRotate.transform.eulerAngles.y, objectToRotate.transform.eulerAngles.z);
        }

        objectToRotate.transform.rotation = targetRotation;
    }
}
#

I'm a beginner and got most of this from a tutorial -_-

#

its not a smooth rotation and it still does the bug i mentioned previously

timber dagger
twilit moat
#

gotcha!

#

i made a little video :))

#

specifically when its on the yellow side is where i have the biggest issue with current code. it turns clockwise instead of going up when W is pressed

#

ignore the grey blocks they serve no purpose yet ! they are the kids of the parent basic cube

timber dagger
#

probably some weird euler angle conversion?

#

iirc i've heard that you should store your own rotation in euler angles because transform.eulerAngles is sometimes inconsistent because quaternions are weird

#

actually it might be due to the order of how rotaitons are applied..?

#

-# rotation is weird D:

twilit moat
#

Ohhh I see! How would I go about that? 🥲

#

ill start googling

timber dagger