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

convert angles to radiants

Discussion in 'Scripting' started by Peeta, Aug 2, 2014.

  1. Peeta

    Peeta

    Joined:
    Jan 25, 2014
    Posts:
    39
    I want to rotate an object 90 degrees because doing Mathf.PI / 2 me it always turns 180 degrees?
    Code (csharp):
    1.  
    2. ogg.transform.rotation.z = Mathf.PI / 2;
    3.  
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    transform.rotation does not use radians, it is a Quaternion
    http://docs.unity3d.com/ScriptReference/Quaternion.html
    You should never directly set the x,y,z and w of a rotation.

    As for actually converting degress to radians, Mathf has consts to do this
    float degrees = 30;
    float radians = degrees * Mathf.deg2rad;
     
  3. Peeta

    Peeta

    Joined:
    Jan 25, 2014
    Posts:
    39
    so there are ways to rotate the GameObject with radians?
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    First you need to pick the correct method. There are many that can be used for rotations:
    http://docs.unity3d.com/ScriptReference/Transform.RotateAround.html
    http://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html

    Or if you want to work with quaternions, you may use:
    http://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html
    http://docs.unity3d.com/ScriptReference/Quaternion.Euler.html

    All those methods work with degrees. If you want to store your values as radians, that's not a big deal, you just need to multiply them when you get and set them as mentioned by @hpjohn.