Updated Unity build and now moving platforms are broken?

I have a bunch of asteroids that act as moving platforms. They move off the left edge of the screen, and reappear at the right side of the screen. They have very simple code that looks like so:

private void FixedUpdate() {
        Vector3 new_position = new Vector3(transform.position.x + _velocity.x * Time.deltaTime, transform.position.y + _velocity.y * Time.deltaTime, 0f);
        _rigidbody2d.MovePosition(new_position);
       
        // Check if we looped to right side
        if (transform.position.x < _boundary.x) {
            float amt_boundary_was_crossed = Mathf.Abs(_boundary.x - transform.position.x);
            transform.position = new Vector3(_line_start.x + amt_boundary_was_crossed, transform.position.y, 0f);
        }
    }

I had them working in the previous version of Unity I was using (2022.1.1). However, I recently upgraded to a new version of Unity (2023.2.20), and notice that the moving platform asteroid now behave erroneously.

before::

after:

Before, the asteroids moved in an orderly fashion, all moving at the same speed, never colliding with eachother. Now, it looks like the asteroids are now possibly colliding against other objects on the loop back. Strangely, disabling the collider2d, setting rigidbdy.isKinematic to true, or changing the object’s collision layer doesn’t seem to prevent this strange bumping.

Was there a major change to Unity’s physics in the past couple of years? Does anyone know what this is?

Nope but the Rigidbody2D has had a “body type” property for a very, very long time which replaced “IsKinematic” property.

Kinematic objects do not have any kind of collision response at all so if you’ve set them to be Kinematic, they cannot and never will collide; the physics system (Box2D) doesn’t support it so something else is wrong here. If you don’t have a collider then clearly they are not colliding either.

Note that calling MovePosition doesn’t change the Transform. It only works after the simulation step has completed so checking it immediately after calling it is wrong. I’d at least check it before you issue a MovePosition and if it’s out of bounds then just adjust its position with calling MovePosition. You shoudl also use the body position, not the Transform position.

I actually have no idea why you’re even using MovePosition. If you set a velocity then it’ll always move at that velocity if it’s Kinematic. Nothing will ever change it unless you do.

1 Like

It seems I need to update my unity bag of tricks. I took your advice and:

  • set rigidbody velocity just once in init
  • changed all instances of transform.position to rigidbody2d.position
  • perform the loopback by setting rigidbody2d.position directly

And that seems to have done the trick. Thank you very much!

1 Like