I was facing another rotation problem yesterday, that i managed to solve by rotating the object like this :
transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World);
I tried almost every other way but nothing seems to work, usually i only use Euler angles to rotate my objects but in this case i couldn't.
So what am trying to accomplish here is to rotate the cube 90 degree around the X or Z axe everytime the player press the arrow key (doesn't have to stay pressing), and theoretically i thought this would work :
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
targetEul = transform.eulerAngles;
targetEul.x += 90;
timer = 90;
//also tried this alone:
targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
}
if (timer>0){
timer -= speed * Time.deltaTime;
transform.Rotate (0, 0, speed * Time.deltaTime, Space.World);
}else{
transform.rotation = Quaternion.Euler (targetEul);
}
This is to avoid the risk of getting the object rotation out of sync which will eventually happens if i don't use that line inside the else statement : GIF PREVIEW
Now if i use that to adjust the rotation, this happens : GIF PREVIEW
in the rotation number: 4 the object start to flip itself strangely, which i have no idea why.
So what i exactly need is this, say am working with just a float that i want to increase in time to reach 10.00, if i do this :
if (x<10){
x+=Time.deltaTime;
}
Then the final result of x won't be exactly 10.00, it would be something a little bit more. and so i need to make it like this :
if(x<10){
x+=Time.deltaTime;
} else{
x=10;
}
That is exactly what i am trying to make but instead of X i want the object rotation, and instead of 10 i want the previous object rotation+90 on X or Z
Is it really "impossible" to do in that simple way?
Thank you very much
EDIT here is the entire Class am using in this example :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class RotateCube : MonoBehaviour
{
public Text txt;
public float speed;
public Vector3 targetEul, myEul;
public bool canRotate, doLeft, doRight;
int i ;
void Update ()
{
//check input when possible
if (canRotate)
checkInput ();
if (doLeft)
checkLeft ();
if (doRight)
checkRight ();
}
//rottate left
float chrono;
void checkLeft ()
{
if (chrono > 0) {
chrono -= speed * Time.deltaTime;
transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World);
myEul.x += speed * Time.deltaTime;
} else {
transform.rotation = Quaternion.Euler (targetEul);
//transform.rotation = Quaternion.Slerp(transform.rotation,targetQuat,100);
canRotate = true;
doLeft = false;
}
}
//rotate right
float chronoR;
void checkRight ()
{
if (chronoR > 0) {
chronoR -= speed * Time.deltaTime;
transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
myEul.z += speed * Time.deltaTime;
} else {
transform.rotation = Quaternion.Euler (targetEul);
//transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
canRotate = true;
doRight = false;
}
}
//check input
Quaternion targetQuat;
void checkInput ()
{
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
//targetQuat = transform.rotation * (Quaternion.AngleAxis (90,Vector3.left));Quaternion.Euler (transform.rotation.eulerAngles.x + 90, 0, 0);
targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);//Quaternion.AngleAxis(90,Vector3.right);
targetEul = transform.eulerAngles;
targetEul.x += 90;
chrono = 90;
canRotate = false;
doLeft = true;
i++;
}
if (Input.GetKeyDown (KeyCode.RightArrow)) {
targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z + 90);
targetEul = transform.eulerAngles;
targetEul.z += 90;
chronoR = 90;
canRotate = false;
doRight = true;
i++;
}
txt.text = "" + i;
}
}
if (time >= 1/60 )
instead ofif (time > 0)
\$\endgroup\$if (time >= 1/60 + 0.01f)
? \$\endgroup\$targetEul.x += 90;
betargetEul.z += 90;
instead ? you seem to want to rotate according to Z axis here:transform.Rotate (0, 0, speed * Time.deltaTime, Space.World);
\$\endgroup\$