Movement with Update vs FixedUpdate

So, I’m not having a problem but I’m curious about something.

public class RubyController : MonoBehaviour
{
    Rigidbody2D rigidbody2D;
    float horizontal;
    float vertical;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        vertical = Input.GetAxis("Vertical");
        horizontal = Input.GetAxis("Horizontal");
    }

    private void FixedUpdate()
    {
        Vector2 position = transform.position;
        position.y = position.y + (3.0f * vertical * Time.deltaTime);
        position.x = position.x + (3.0f * horizontal * Time.deltaTime);

        rigidbody2D.MovePosition(position);
    }
}

The above works for what I need it to do - keep in mind I’m doing the 2D Beginner Tutorial Ruby’s Adventure.
But the code below causes Ruby to move extremely slow, and I’m curious why that’s the case.

public class RubyController : MonoBehaviour
{
    Rigidbody2D rigidbody2D;
    float horizontal;
    float vertical;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        vertical = Input.GetAxis("Vertical");
        horizontal = Input.GetAxis("Horizontal");
   
        Vector2 position = transform.position;
        position.y = position.y + (3.0f * vertical * Time.deltaTime);
        position.x = position.x + (3.0f * horizontal * Time.deltaTime);

        rigidbody2D.MovePosition(position);
    }
}

Here is where Debug.Log() can come in handy. Print the amounts that you’re moving Ruby by!

For introspecting new code, I recommend liberally sprinkling Debug.Log() statements through the code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized? What are their values?

Etc. This is one of the best ways to rapidly gain intimate knowledge of how a game works.

If I had to guess, perhaps friction is coming into play more, or perhaps there’s a frame timing issue at play, or the rigidbody is accumulating velocity somehow that is countering your motion.

ALSO, specific to the above case, here is some timing diagram help:

That kinda illustrates why you want to move RB-related stuff in FixedUpdate, as it correlates precisely to “pumps” of the Physics systems.