Controlling a rotating object

I have this method which (theoretically) should keep my 3d game object rotating back and forth. But it is only rotating the object forward.

Here is a diagram of how it is supposed to go:

[19290-cube+rotation.png|19290]

And here is the code snippet:

    public void RotateObjectForwardBack()
    {
        switch (currentMovementTask)
        {
            case MovementTask.rotateBlockForward:
                transform.Rotate(Vector3.left, 45 * speed * Time.deltaTime);
                if (_cubeObject.transform.rotation.x > 90)
                    currentMovementTask = MovementTask.rotateBlockBackwards;
                break;
            case MovementTask.rotateBlockBackwards:
                transform.Rotate(Vector3.right, 45 * speed * Time.deltaTime);
                if (_cubeObject.transform.rotation.x < -90)
                    currentMovementTask = MovementTask.rotateBlockForward;
                break;
        }
    } 

Can anyone help me figure out what my problem is? Many thanks in advanced!

2 Answers

2

I guess you want _cubeObject.transform.eulerAngles.x instead of _cubeObject.transform.rotation.x since rotation returns a quaternion.

Thank you, that fixed it!

You’re switch case does not make sense. Your variable is currentMovementTask however your condition is a variable based on some other variable, you should use the currentMovementTask as your condition. Then when your if statement works set currentMovementTask to 2 to make it go backwards.

Yeah and that is exactly how I have it set up, cause I also have other methods that handle translation and it does it very well but for some reason it doesn't seem to respond to my if statement when it comes to rotation.