- Home /
90 Degree stopping rotation on y-axis issue
Hello there,
I am facing a weird rotation issue. I try to rotate a cube 90 degree around y-axis after input. It works properly on first 3 inputs but after that, it starts rotating continuously. I check rotation by angle and the issue is, if it gets negative value it spins infinitely.(Angles process like 315 -> 225 -> 135 -> 45 issue is below 45)
There is code for context;
public class CubeMovement : MonoBehaviour {
Vector3 firstPosition;
Vector3 secondPosition;
Vector3 initRot;
bool flag;
int count = 0;
// Use this for initialization
void Start () {
initRot = transform.localRotation.eulerAngles;
flag = false;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
firstPosition = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0)) {
secondPosition = Input.mousePosition;
initRot = transform.localRotation.eulerAngles;
if (secondPosition.x > firstPosition.x) {
count++;
flag = true;
}
/*
if (secondPosition.y > firstPosition.y) {
Debug.Log("Up swipe");
gameObject.transform.rotation = Quaternion.Euler(new Vector3(180, 0, 180));
}
else if(secondPosition.y < firstPosition.y){
Debug.Log("Down swipe");
gameObject.transform.rotation = Quaternion.Euler(new Vector3(-180, 0, -180));
}
*/
}
//transform.rotation = Quaternion.AngleAxis(45, transform.up) * transform.rotation;
if (flag) {
if (transform.localRotation.eulerAngles.y > initRot.y - 90) {
//transform.Rotate(Vector3.up, Time.deltaTime * -90, Space.World);
Quaternion newRotation = Quaternion.AngleAxis(-90, Vector3.up) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, .05f);
Debug.Log(transform.localRotation.eulerAngles.y);
}
else {
if (count % 2 == 0)
transform.rotation = Quaternion.Euler(initRot);
flag = false;
}
//transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(new Vector3(0, 0, 90)), Time.deltaTime );
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Creating a multiple part turret what locks onto certain axis. 4 Answers
How to get angular difference? 2 Answers
Unity Simulate Local Rotation 0 Answers
Align GameObject to Terrain angle 2 Answers