Search Unity

GameObject following the player shakes with each movement

Discussion in '2D' started by Davrozz, May 20, 2022.

  1. Davrozz

    Davrozz

    Joined:
    May 15, 2022
    Posts:
    8
    Hi all,

    I am trying to make a gameObject (another character) copy the exact moves of the player. I am using the script from this solution: https://gamedev.stackexchange.com/questions/178164/2d-party-follow-the-leader-in-unity

    However, every time the player moves, the follower keeps shaking. I have tried using movement functions like Lerp, MoveTowards and SmoothClamp.

    Here is a short gif of the problem (look at the following bunny): https://im2.ezgif.com/tmp/ezgif-2-ba2fb638c3.gif

    Any ideas as to what i can do to smooth the movement?
    Thanks in advance.
     

    Attached Files:

  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hello,

    I tried the script out in Unity 2021.3.2f1 and it works great. The movement is smooth and I'm not sure what's causing your movement to be jittery like that. If you're using Rigidbody2Ds, you can try setting "Interpolate" to "Interpolate" and see if that helps. Also, if both objects have collision, make sure they aren't colliding with each other.

    I doubt it's an editor only issue, but you could try building your game to see if the jitter persists.

    If all else fails, I would recommend trying out the script on two basic sprite objects to see if that works, and then try and figure out what's causing the jitter by messing with your player. It could be something in the player controller, or some other component.
     
    Davrozz likes this.
  3. Davrozz

    Davrozz

    Joined:
    May 15, 2022
    Posts:
    8
    Thanks for your reply! I tried all options, but it is still just as jittery. I already made sure they were not colliding, and had set it to interpolate. I even removed the rigidbody2d and box collider from the follower , but nothing worked.

    I created a new project with two sprites, a tilemap, two scripts (simple player and follower script) and it does exactly the same. Building the game does not work either. ..Any ideas left? :)
     
  4. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    In a new project, can you try just having two sprites with the follower script on one of them? You can test it by moving the leader sprite around in scene view. It could be the player script messing with the movement. If that doesn't work, could you tell me what version of Unity you're using? I can see if I can reproduce the jitter there.
     
    Davrozz likes this.
  5. Davrozz

    Davrozz

    Joined:
    May 15, 2022
    Posts:
    8
    Hi! So I did what you advised, and that seems to work fine. So I guess it has to do with my PlayerController? Still weird then, that the player's movement seems fine. I tried using RigidBody2D.velocity and RigidBody2D.AddForce but with both the follower is still shaking. Not, however, when I drag them in the scene view. Any clue as to how I should make my player script then?
     
  6. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    So I had tested it with a very basic script I wrote:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Movement : MonoBehaviour
    4. {
    5.     [SerializeField] float movementSpeed = 10f;
    6.     [SerializeField] float jumpSpeed = 10f;
    7.  
    8.     Rigidbody2D rb;
    9.  
    10.     void Awake()
    11.     {
    12.         rb = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         rb.velocity = new Vector2(Input.GetAxis("Horizontal") * movementSpeed, rb.velocity.y); // Movement.
    18.  
    19.         if (Input.GetKeyDown(KeyCode.W)) // Jump.
    20.         {
    21.             rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
    22.         }
    23.     }
    24. }
    This is obviously not a very good player controller, but I did get smooth movement with this. You can test with this script to see if you still get any jittering, and if not, you can try slowly adding to this to see what breaks it.

    Something I noticed while testing is that adding a Rigidbody2D component to the follower object creates various issues, so I left that object without any collision or physics.
     
    Davrozz likes this.
  7. Davrozz

    Davrozz

    Joined:
    May 15, 2022
    Posts:
    8
    Hi! I tried this out, once again on a new project, but for some reason the issue still persists. I am using Unity version 2021.3.2f1. For now I'll just deal with the fact that it shakes. Thank you for your help nonetheless!
     
  8. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Sorry we couldn't solve it. That's really strange.
     
    Davrozz likes this.
  9. Jakub_Machowski

    Jakub_Machowski

    Joined:
    Mar 19, 2013
    Posts:
    647
    I noticed the same on unity 2021.3.25, when moving any object in playmode or object is moving cause its parent of moving object it looks like that object vertexes are modified and it is shaking. That make a lot of huge problems hope it could be fixed