Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Bug Updated Unity build and now moving platforms are broken?

Discussion in 'Physics' started by qtran122, May 13, 2024.

  1. qtran122

    qtran122

    Joined:
    Dec 16, 2013
    Posts:
    8
    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:

    Code (CSharp):
    1. private void FixedUpdate() {
    2.         Vector3 new_position = new Vector3(transform.position.x + _velocity.x * Time.deltaTime, transform.position.y + _velocity.y * Time.deltaTime, 0f);
    3.         _rigidbody2d.MovePosition(new_position);
    4.        
    5.         // Check if we looped to right side
    6.         if (transform.position.x < _boundary.x) {
    7.             float amt_boundary_was_crossed = Mathf.Abs(_boundary.x - transform.position.x);
    8.             transform.position = new Vector3(_line_start.x + amt_boundary_was_crossed, transform.position.y, 0f);
    9.         }
    10.     }
    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?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
    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.
     
    qtran122 likes this.
  3. qtran122

    qtran122

    Joined:
    Dec 16, 2013
    Posts:
    8
    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!
     
    MelvMay likes this.