Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[C#] Rotating 2D object from any angle to 0

Discussion in 'Scripting' started by pKallv, May 21, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I am pretty new to Unity and C# and this sound like a simple thing to do but i still have problem to get this to work properly.

    I have a few object with different rotation angles and at a certain point i want to rotate each object to the default rotation (for the test 0).

    I do get the object to rotate but not stop were i want it. Would someone nice be able to show me a script that rotate an object (z) from whatever rotation to the default?

    I guess i also have some problem to find the correct stopping angle, it seems like local/world position cause that.

    Here is the code, in Update(), that i am playing around with. The commented lines are different options i have tested:
    Code (csharp):
    1.         if (isRotate) {
    2.  
    3. //=== The different tests
    4.  
    5. //          dummyGameObject.transform.Rotate(0, 0, myRotationSpeed * Time.deltaTime * posOrNeg);
    6. //          dummyGameObject.transform.Rotate(Vector3.forward * Time.deltaTime * 800);
    7. //          dummyGameObject.transform.Rotate(0,20 * Time.deltaTime * 500, 0);
    8. //          dummyGameObject.transform.rotation = Quaternion.AngleAxis(30, Vector3.up);
    9. //          dummyGameObject.transform.RotateAround (dummyGameObject.transform.position, Vector3.forward, 1000 * Time.deltaTime);
    10. //          dummyGameObject.transform.Rotate(Vector3.forward, 270 * Time.deltaTime);
    11.  
    12.  
    13.             print (dummyGameObject.transform.rotation.z);
    14.  
    15.             // Check rotation.z if it is 0, if so stop animation
    16.             if (dummyGameObject.transform.rotation.z == 359) {
    17.                 isRotate = false;
    18.            
    19.             }
    20.         }
     
  2. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122