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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Help understanding interpolation for transforms in Update()

Discussion in 'Scripting' started by CaveTurtle, Oct 28, 2018.

  1. CaveTurtle

    CaveTurtle

    Joined:
    Oct 27, 2018
    Posts:
    12
    Hello eveyone. :) I'm new to Unity but come from a C++ background and have experience with engine programming. I'm trying to get a grasp on interpolation outside of using a rigidbody, and by interpolating transforms under the Update() method.

    In my experience programming engines with fixed time steps I would track the object position for logic purposes and drawing purposes. The drawing position has no relation to any actual collision or logic, but is used strictly for interpolation. Every update loop I would keep track of the prior position of the object, then move it. Once I hit the draw loop (runs on a variable rate which is why interpolation is needed) I would us something like this:

    // Draw Part
    interpolation = float(mainClock.getElapsedTime().asMilliseconds() + skipTicks - nextTick) / float(skipTicks);

    Then I would set the draw position before rendering the graphic:

    object.setDrawingPosition(objectPriorPos.x + ((objectCurrentPos.x - objectPriorPos.x) * interpolation, objectPriorPos.y + ((objectCurrentPos.y - objectPriorPos.y) * interpolation, objectPriorPos.z + ((objectCurrentPos.z - objectPriorPos.z) * interpolation);

    Then I draw the object based on the interpolated position (DrawingPosition).

    --

    I was reading up how doing this in Unity and it appears all I can find is using something like this with Vector3.Lerp:

    // Key Pressed
    targetPosition.x = GameObject.Find("gamePlayer").transform.position.x;
    targetPosition.y = GameObject.Find("gamePlayer").transform.position.y;
    targetPosition.z = GameObject.Find("gamePlayer").transform.position.z + 10f;
    transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime);

    As I'm used to working in my first example above where you work with both the Update and Render loops, is the Vector3.Lerp the proper way to keep smooth movement? It doesn't look like it's interpolating the actual render because it's actually editing the object's position that is also used for logic. Maybe I'm confused here... as I'm coming from a different work flow. I just want to interpolate the render for smoothness in the event frames are dropping outside of my Application.targetFrameRate = 60;

    I want to interpolate the visual aspect of the object, but never use a variable value like deltaTime to have any part in the movement itself as a low FPS can cause issues with collision checks.

    I'm not a fan of "variable time steps", and have mostly used "fixed" with variable render rates.

    Thanks for any help understanding what I need to do! :)

    PS. I'm also not interested in doing these under FixedUpdate() as my understanding is only physics related tasks should be handled here. I just want to keep my movement frame dependent but with interpolation on the visual side. :) Thank you.
     
    Last edited: Oct 28, 2018
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    I used to write engines like that too - the physics would run at 30fps and the rendering engine would just interpolate between the last 2 positions for a smooth render. Unity doesn't work that way though (unless you use the new low-level rendering architecture to have finer control).

    Your best solution is just to enable https://docs.unity3d.com/ScriptReference/Rigidbody-interpolation.html rigidbody interpolation which will do the same job. If you run FixedUpdate physics at 30fps it'll smooth it out to render speed.
     
    Opeth001 and CaveTurtle like this.
  3. CaveTurtle

    CaveTurtle

    Joined:
    Oct 27, 2018
    Posts:
    12
    Thank you! :) I guess what I'll do is anything that actually needs to move that is visible to the player will be done under FixedUpdate as a RigidBody using the Interpolation setting.

    I'm really liking how fast I can work in Unity, it's just a struggle coming from a background in which I could use any desired Time Step and implement it the way I want to using something already established.
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    Yeah, there’s a trade off between fine control of the low level stuff but the other features make things much faster to develop.

    You can set your fixed update timestep in the project settings too (the physics tick speed). FixedUpdate calls work at this speed so you can put your game logic there to move bodies.
     
    CaveTurtle likes this.
  5. CaveTurtle

    CaveTurtle

    Joined:
    Oct 27, 2018
    Posts:
    12
    Awesome. :) Thanks again! If I can develop this fast then the trade off is well worth it as long as I can shuffle movement to my FixedUpdate() and maintain smooth movement :)

    I'm really enjoying using Unity as opposed to hard coding everything with C++ and openGL and forcing myself to build custom tool sets regardless of more "control" at the cost of time. :D