- Home /
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:
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!
Answer by robhuhn · Dec 13, 2013 at 04:00 PM
I guess you want _cubeObject.transform.eulerAngles.x instead of _cubeObject.transform.rotation.x since rotation returns a quaternion.
Answer by MFen · Dec 13, 2013 at 03:32 PM
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.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
RotateAround Limitations 2 Answers
Twin Stick Shooter controller 0 Answers
C# How to rotate cube X/Z coor. for top-down game? 2 Answers
Distribute terrain in zones 3 Answers