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

[2D] How to set any degree to rotate to in Quaternion.RotateTowards

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

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    This is probable a simple problem but i am learning. I am trying to rotate a sprite to a set degree using this code, which work but is currently pointing to the "target" (empty GameObject):

    Code (csharp):
    1.  
    2. public Transform target;
    3. public float speed;
    4.  
    5. void Update() {
    6.     float step = speed * Time.deltaTime;
    7.     //static Quaternion RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta);
    8.     transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, step);
    9.     print ("transform.rotation: " + transform.rotation);
    10. }
    I would want to be able to set the direction manually, via a simple parameter, like "degree = 270" but can not get it to work.

    I would really appreciate help with this.
     
  2. Ilseroth

    Ilseroth

    Joined:
    May 18, 2014
    Posts:
    17
    transform.eulerAngles = new Vector3(0, 0, 0 );
    easiest way i found to set to an exact angle,
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Ilseroth, thanks. I just found out the same thing myself using this code:

    Code (csharp):
    1.  
    2. void Update() {
    3.     float step = speed * Time.deltaTime;
    4.     //static Quaternion RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta);
    5.     transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,0), step);
    6.     print ("transform.rotation: " + transform.rotation);
    7.     }
    8.