MovePosition has different behavior depending on the machine

I’ve noticed that the following code leads to completely different behaviors on different machines:

rb2d = GetComponent<RigidBody2D>();
//Move GameObject up
rb2d.MovePosition(rb2d.position + Vector2.up * Time.deltaTime);

Editor: Moves at the correct speed
Build on my Windows: Moves way too slowly
Build on a Linux machine: Moves way too quickly

Is there any reason for this? Am I doing something wrong?

Where is the code being excuted from? Update or FixedUpdate?

1 Like

I think you just fixed the problem. I had it in Update. :roll_eyes:

Having Time.deltaTime in there should compensate for differences in framerates, though.

I’m not sure why it behaves like this. All I can say is that there’s absolutely no other script on my GameObject at all. It’s supposed to just move up at a set speed. But the speed seems vary from ‘barely moving’ to ‘way too fast’.

Can you put a number on how much faster/slower it’s moving? Is it something straightforward like 2x as fast, for example? Is it a consistent speed?

post your full script or update method.

That is basically the full script. The rest has nothing to do with the speed.

using UnityEngine;
using System.Collections;

public class EnemyRed : MonoBehaviour
{
    private bool movingUp = false;
    Rigidbody2D rb2d;
    public float speed;

    private void Awake()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        if (movingUp)
        {
            rb2d.MovePosition(rb2d.position + Vector2.up * speed * Time.deltaTime);
            if (rb2d.position.y >= 5f)
                movingUp = false;
        }
        else
        {
            rb2d.MovePosition(rb2d.position + Vector2.down * speed * Time.deltaTime);
            if (rb2d.position.y <= -5f)
                movingUp = true;
        }
    }
}

The speed difference is really drastic. The faster one is the Editor (which is correct) and the slower one is the Windows build. I don’t have one from Linux right now, but it was about 2x faster than the left one.

I’ve never used rigidibody moveposition myself but it appears it depends on the rigidbody settings as to how it behaves.

For a simple move like that, I would use waypoints myself.