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. Dismiss Notice

Question Area Effector Conflicting with Moving Rigidbody Position... Causes Jerky Movement

Discussion in '2D' started by DMK_YT, Sep 17, 2023.

  1. DMK_YT

    DMK_YT

    Joined:
    Oct 8, 2020
    Posts:
    10
    I am trying to make a top-down action game, where enemies will use pathfinding to follow the player. My pathfinding system is based upon the one created by Sebastian Lague in his A* Pathfinding series: https://youtube.com/playlist?list=PLFt_AvWsXl0cq5Umv3pMC9SPnKjfp9eGW&si=0IackdURf8WNy9Fl

    In order to move the enemies position to whichever waypoint is on the current path, the script uses this:
    transform.position = Vector2.MoveTowards(transform.position, waypoints[index].position, speedMultiplier * speed * Time.fixedDeltaTime);


    This works fine enough and hasn't caused many issues for me.

    However, I've been trying to implement a "conveyer belt" obstacle in my game, essentially it moves the player and any enemies on it either left or right. This currently works by using the Area Effector 2D

    The issue is, changing the velocity of the gameobject through an Area effector and the rigidbody's position in pathfinding seems to cause somewhat jerky movement.

    In the image below, the character on the conveyer belt (the conveyer belt is the large black strip along the camera view) cannot move forward, and will only be able to lurch forward once the player's position changes as it recalculates the path it needs to take. The character is not being pushed backwards while walking forwards, instead its just stuck in this one spot.

    upload_2023-9-17_15-35-26.png

    I've tried changing the speed, using a script that moves the transform instead of changing the velocity, etc. The player moves fine, but enemies consistently get caught on it.

    Is there a better way of doing this? I don't know how else to do it, and most results for "conveyer belts in Unity" are often related to big Factorio-style games or something; I just want an object that, when you or an enemy walks into it, can push you in a given direction without interrupting your scripted inputs.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Fix this first:

    It is most likely the source of your jitter.

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
  3. DMK_YT

    DMK_YT

    Joined:
    Oct 8, 2020
    Posts:
    10
    Thank you for the response.

    I have changed it to be rb.MovePosition instead of setting the transform. However, this does not seem to have resolved the issue.

    I'll send a video here that shows exactly what the issue is:
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    A Rigidbody2D changes its position acccording to its velocity. Forces change its velocity. That's how all physics engines work.

    If you are writing directly to the transform or body position i.e. ignoring velocity/forces or if you're asking to move to a specific position with MovePosition then how can it actually write the Rigidbody2D position to the Transform? The answer is that it cannot because it cannot move to two different positions at the same time.

    Either you choose to use explicit position control (MovePosition etc) or you let velocity do its magic. You need to choose.