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

Need to rotate object at a constant rate

Discussion in 'Scripting' started by Dazz500, Nov 16, 2014.

  1. Dazz500

    Dazz500

    Joined:
    Nov 13, 2014
    Posts:
    64
    Hi all, I'm wanting to rotate a object through x number of degrees ( where x entered by user ) on the Y axis & as the title says at a constant rate.
    What happens is the rotation starts off slow but increases as the value of the variable " turn" increases. I know this must have something to do with Time.deltatime but I'm not sure how to get round it.
    I know ( well hope ) this is probably quite basic stuff for some of you experienced unity users but this is quite new to me.
    Any is help appreciated, thanks.


    Code (csharp):
    1.  
    2. public class planepilot : MonoBehaviour {
    3.  public float speed;
    4.  private string speedtext = "10" ;
    5.  public int ATCspeed = 10;
    6.  public int newSpeed ;
    7.  private string headingtext = "0" ;
    8.  public int ATCheading = 0;
    9.  public int newHeading;
    10.  public float turn ;
    11.  public bool turncomplete = false ;
    12.  void Start () {
    13.  Debug.Log ("plane script added to :: " + gameObject.name);
    14.  speed = 80.0f;
    15.  }
    16.  
    17.  // Update is called once per frame
    18.  void Update () {
    19.  
    20.  
    21.  transform.position += transform.forward * Time.deltaTime * ATCspeed;
    22.  if (Input.GetKey (KeyCode.Return)) {
    23.  ATCspeed = newSpeed; ATCheading = newHeading ;
    24.  }
    25.  
    26.  //***************** changes heading ***************
    27.  
    28.  if ((transform.eulerAngles.y) < ATCheading) { turncomplete = false;
    29.  
    30.  turn = transform.eulerAngles.y; turn = turn + 0.02f ;
    31.  
    32.  
    33.  
    34.  transform.Rotate (0.0f, turn* Time.deltaTime, 0.0f); Debug.Log(" eulerangles.y = " + (transform.eulerAngles.y) + ( " :: TURN  : " + turn ) +
    35.   ( "  / ATC heading : " + ATCheading ) );
    36.  
    37.  }
    38.  
    39.  if (turncomplete == false) {
    40.  if ((transform.eulerAngles.y) > ATCheading ) {
    41.  transform.eulerAngles = new Vector3 (0.0f, ATCheading, 0.0f);
    42.  turncomplete = true;
    43.  }
    44.  
    45.  }
    46.  
    47.    }
    48.  
    49.  
     
  2. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    The function is not taking an absolute angle, but a rotation angle, so you should have an angle (0.02f ?) multiplied by Time.deltaTime, and not add the current angle to it.
    Unless I am a really bad reader of the documentation's example, which is of cource possible.
    http://docs.unity3d.com/ScriptReference/Transform.Rotate.html

    Hope it helps anyway.
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you want to turn it constantly, you musn't change 'turn'. It will then always add turn*Time.deltaTime to your current rotation.
     
  4. Dazz500

    Dazz500

    Joined:
    Nov 13, 2014
    Posts:
    64
    Thanks guys I'm now using

    Code (csharp):
    1.  
    2. transform.Rotate(Vector3.up*10* Time.deltaTime);
    3.  
    Which has solved that problem.