jikaviri
jikaviri

Reputation: 33

How to Rotate on Y Axis by 90 degrees left or right?

I created this code but it doesn't work and i don't understand why. In the Debug.Log everything seems fine but the while loops are true for ever. Is there a better way to Rotate the gameobject by 90 degrees left and right?

https://www.youtube.com/watch?v=7skGYYDrVQY

 //Public
 public float rotateSpeed = 150f;
 public bool isMoving = false;

 //Private
 private Rigidbody myRigidbody;


 void Start () {

     myRigidbody = GetComponent<Rigidbody> ();
 }

 void Update() {

     Quaternion originalRotation = this.transform.rotation;

     if (!isMoving) {


         if (Input.GetButtonDown ("PlayerRotateRight")) {

             //Rotate Right
             StartCoroutine (PlayerRotateRight (originalRotation));

         } else if (Input.GetButtonDown ("PlayerRotateLeft")) {

             //Rotate Left
             StartCoroutine (PlayerRotateLeft (originalRotation));
         }
     }

 }

 IEnumerator PlayerRotateRight(Quaternion originalRotation) {

     Quaternion targetRotation = originalRotation;
     targetRotation *= Quaternion.AngleAxis (90, Vector3.up);

     while (this.transform.rotation.y != targetRotation.y) {

         Debug.Log ("Current: " + this.transform.rotation.y);
         Debug.Log ("Target: " + targetRotation.y);

         isMoving = true;
         transform.rotation = Quaternion.RotateTowards (transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
         yield return null;
     }

     isMoving = false;
 }

 IEnumerator PlayerRotateLeft(Quaternion originalRotation) {

     Quaternion targetRotation = originalRotation;
     targetRotation *= Quaternion.AngleAxis (90, Vector3.down);

     while (this.transform.rotation.y != targetRotation.y) {

         Debug.Log ("Current: " + this.transform.rotation.y);
         Debug.Log ("Target: " + targetRotation.y);

         isMoving = true;
         transform.rotation = Quaternion.RotateTowards (transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
         yield return null;
     }

     isMoving = false;
 }

Upvotes: 1

Views: 2062

Answers (2)

Bizhan
Bizhan

Reputation: 17145

You are comparing quaternions not euler angles. And also it is better to have a threshold since you are comparing two floats.

Just change

while (this.transform.rotation.y != targetRotation.y)

to

while (this.transform.eulerAngles.y != targetRotation.eulerAngles.y)

or

while (Mathf.Abs(this.transform.eulerAngles.y - targetRotation.eulerAngles.y) > THRESHOLD)

where THRESHOLD is a constant small enough. Also set the rotation to its exact angle after the loop in order to prevent rotation errors.

Upvotes: 0

    if (Input.GetKeyDown (KeyCode.A)) {
        transform.Rotate (0, -90, 0, Space.World);


    }else if(Input.GetKeyDown(KeyCode.D)){
        transform.Rotate(0,90,0, Space.World);
    }

Based on what your saying, something as simple as this should work perfectly fine. Any particular reason you chose to do it that complicated? In that case, i could modify the answer to fit your code.

Upvotes: 2

Related Questions