- Home /
Rotate a Cube by 90 degree
Hello,
I have a simple cube, with a camera in front of him. So in the game render we just see a face of the cube. And when i click on the arrows (up,down,left or right) i want the cube rotate by 90 degree in the direction of the arrow to show an other face of the cube.
The probleme is, for example when a first rotate the cube to the left, and i want to rotate it to the up, it will rotate to the up but relative to the first position of the cube. Like this:
I use DOTween for the rotation (and i have the same result with iTween). And i try locale rotate with a parent and normal rotate without parent. The result is the same. For rotating the cube, i get the x,y and z rotate values like this:
Vector3 v3 = transform.rotation.eulerAngles;
xCube = v3.x;
yCube = v3.y;
zCube = v3.z;
and when an arrow is clicked:
void left(){
transform.DOLocalRotate(new Vector3(xCube, yCube+90, zCube), 1);
}
void right(){
transform.DOLocalRotate(new Vector3(xCube, yCube-90, zCube), 1);
}
void up(){
transform.DOLocalRotate(new Vector3(xCube+90, yCube, zCube), 1);
}
void down(){
transform.DOLocalRotate(new Vector3(xCube-90, yCube, zCube), 1);
}
I'm new with Unity and 3D. I understand WHY the cube rotate this way, but i don't understand how to do what i want.
Can you help me ?
what is this DoLocalRotate? use tranform.Rotate ins$$anonymous$$d.
void Left()
{
transform.Rotate(new Vector3(90f, 0f, 0f));
}
EDIT: ok, I've noticed you are using DOTween. Use DORotate ins$$anonymous$$d DOLocalRotate.
Check out the documentation for DORotate. There is a third parameter Rotate$$anonymous$$ode for that method.
I tried Rotate$$anonymous$$ode.WorldAxisAdd but it give me weird results. I don't think this is what i'm looking for. There's no way to set the rotation point to 0,0,0 when i rotate my object ? This way, every time i rotate my cube, the values will return to 0. Like i'm always see the 1st face of the cube, but it's another. It would be perfect but i don't know if you can do this... ?
try this way void Left() { transform.DORotate(new Vector3(0,90,0), 1, Rotate$$anonymous$$ode.WorldAxisAdd); }