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

Movement speed is being affected by the frame rate despite using Time.deltaTime.

Discussion in 'Scripting' started by EliteWalrus, Nov 15, 2014.

  1. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Code (JavaScript):
    1.     //Movement
    2.     var walk : float = Input.GetAxis ("Vertical") * walkSpeed;
    3.  
    4.     if (Input.GetKey (KeyCode.W)) {
    5.         rigidbody.MovePosition (transform.position + transform.right * walk * -1 * Time.deltaTime);
    6.     }
    7.     if (Input.GetKey (KeyCode.S)) {
    8.         rigidbody.MovePosition (transform.position + transform.right * walk * -1 * Time.deltaTime);
    9.     }
    What am I doing wrong? Thanks in advance.
     
    Last edited: Nov 15, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Update or FixedUpdate?
     
  3. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Update
     
  4. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    What happens if you change the + into - and remove that "-1"?

    I tend to put parenthesis around the '-' on those calculations, to be really sure that it does not interpret it as a subtraction.

    Not saying it will change a thing, but it might.
     
  5. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Thanks for the tip but unfortunately you were right, it didn't change anything.
     
  6. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Moving it to FixedUpdate solved the the problem. I used this code.
    Code (JavaScript):
    1. function FixedUpdate () {
    2.     //Movement
    3.     var walk : float = Input.GetAxis ("Vertical") * walkSpeed;
    4.     if (walk > 0 || walk < 0) {
    5.         rigidbody.MovePosition (transform.position - transform.right * walk * Time.deltaTime);
    6.     }
    7. }    
     
  7. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Rigidbodies get updated on FixedUpdate. You register them to be updated in Update, tho they will carry the old position untill a FixedUpdate kicks in, updating the position to the one last set by an Update. Make sure to handle anything related to rigidbodies in FixedUpdate in the future.