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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Update vs FixedUpdate Transform positions

Discussion in 'Physics' started by Starducky, Aug 4, 2015.

  1. Starducky

    Starducky

    Joined:
    Aug 1, 2015
    Posts:
    5
    I'm having trouble understanding this problem. I have a Debug.DrawRay function in Update and FixedUpdate, both originating from the player's transform.position. However, they appear to be at different locations on the screen while the character is in motion. See attached image for details. In this case, my character is falling and moving towards the right. As force/velocity increases, so does the distance between the Update and FixedUpdate rays. The rays under the capsule are being rendered in Update, the rays on the left are rendered in FixedUpdate. What's going on here?

    Quick edit: I'm moving the character by using rigidbody.AddForce; the character is falling due to gravity. Debug.DrawRay is the last thing to be called in both Update and FixedUpdate.
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    The only valid reason why they'd be different that I can think of is that you have a Rigidbody with inter/extra-polation on. In this case, it's expected that the transform is different during update as it's interpolating/extrapolating its position.

    However, unless it's moving really fast, I wouldn't expect a large difference.
     
    IceKapper likes this.
  3. Starducky

    Starducky

    Joined:
    Aug 1, 2015
    Posts:
    5
    Yeah, I'm not sure what's going on. It doesn't seem to be affecting the actual physics itself, at least not to the extent that I can observe. Interpolation is currently set to none. This problem is noticeable even at low velocities - you can see it pretty much as soon as the character starts moving.

    After looking around online, it seems that other people are having this problem as well. I read somewhere that adding this inside of the FixedUpdate DrawRay can fix the position problem:
    Code (CSharp):
    1. Debug.DrawRay(transform.position + m_rigidbody.velocity * Time.fixedDeltaTime, input.moveInput.normalized, Color.green)
    This seems to imply that the Draw is somehow one step behind the current FixedUpdate step? I'm not familiar enough with Unity's updating to work out what's going on...

    EDIT: I typed the code incorrectly before, I have edited it so that it makes sense now.
     
    Last edited: Aug 6, 2015
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    What do your Debug.DrawRay function calls look like right now?
     
  5. Starducky

    Starducky

    Joined:
    Aug 1, 2015
    Posts:
    5
    In both Update and FixedUpdate, they looked like this:
    Code (CSharp):
    1. Debug.DrawRay(transform.position, input.moveInput.normalized, Color.green);
    With the 'fix' I found, the code in my FixedUpdate now looks like this:
    Code (CSharp):
    1. Debug.DrawRay(transform.position + rigidbody.velocity * Time.fixedDeltaTime, input.moveInput.normalized, Color.green);
    Update still looks the same as before.

    Upon reviewing my previous post, I realized I incorrectly typed the function in the post (it is actually correct in my code). I will go back and edit that.
     
  6. Zahidylin_Marat

    Zahidylin_Marat

    Joined:
    Jul 28, 2012
    Posts:
    114
    The same problem
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    Note the last post here was in 2015 and it's not clear exactly what your problem is or even if you need help. If you require help then I suggest you ellaborate here or create a new thread.