- Home /
How to fix bouncing, jittery effect between two dynamic objects in 2D
I'm brand new to Unity as well as coding in C# in general so apologies in advance for any lack of clarity.
I have two player objects (black and yellow) in a top down 2d situation that are movable with the arrow keys. You can switch between which one you control by pressing the "e" key. Whichever object currently being controlled by the player is automatically followed by the other one, which is set to move closer if a certain distance between the two is reached.
I'd like to keep both rigidbodies dynamic rather than kinematic (at least most of the time) so they can bump into and push each other with Unity's built in physics, but I think the automatic "follow" command is causing some bouncy/jittery motion when the active object pushes the other.
Looking for any possible solutions to this, preferably something that would let me utilize the built in dynamic physics as much as possible. Thanks to anyone in advance!
link to screen recording of the scene here
void FixedUpdate()
{
if(BlackActive)
{ //animate Black first
if(bmovement != Vector2.zero)
{
animator.SetFloat("moveX", bmovement.x);
animator.SetFloat("moveY", bmovement.y);
//animator.SetBool("blackmoving", true);
}
else
{
//animator.SetBool("blackmoving", false);
}
//make follower's speed faster so they don't lag behind
ymoveSpeed = 7f;
bmoveSpeed = 5f;
//move Black
BRB.MovePosition(BRB.position + bmovement * bmoveSpeed * Time.fixedDeltaTime);
//if distance between Black and Yellow is > "playerfollowgap"
if(Vector2.Distance(yellowtarget.position,transform.position) > playerfollowgap)
{
//animate Yellow second
if(ymovement != Vector2.zero)
{
animator.SetFloat("yellowmoveX", ymovement.x);
animator.SetFloat("yellowmoveY", ymovement.y);
//animator.SetBool("yellowmoving", true);
}
else
{
//animator.SetBool("yellowmoving", false);
}
//make Yellow follow Black
transform.position = Vector2.MoveTowards
(transform.position, yellowtarget.position, ymoveSpeed * Time.deltaTime);
}
}
else
{
if(YellowActive)
{
if(ymovement != Vector2.zero)
{
animator.SetFloat("yellowmoveX", ymovement.x);
animator.SetFloat("yellowmoveY", ymovement.y);
//animator.SetBool("yellowmoving", true);
}
else
{
//animator.SetBool("yellowmoving", false);
}
bmoveSpeed = 7f;
ymoveSpeed = 5f;
YRB.MovePosition(YRB.position + ymovement * ymoveSpeed * Time.fixedDeltaTime);
if(Vector2.Distance(blacktarget.position,transform.position) > playerfollowgap)
{
if(bmovement != Vector2.zero)
{
animator.SetFloat("moveX", bmovement.x);
animator.SetFloat("moveY", bmovement.y);
//animator.SetBool("yellowmoving", true);
}
else
{
//animator.SetBool("yellowmoving", false);
}
transform.position = Vector2.MoveTowards
(transform.position, blacktarget.position, bmoveSpeed * Time.deltaTime);
}
}
}
}
Answer by MUG806 · Feb 15, 2021 at 02:46 PM
I'm guessing the issue is that you are manually setting the positions of the objects. With rigidbodies you are supposed to only move them by adding forces or using other inbuilt methods. I think your use of: "MovePosition()" is good, but you shouldn't be using "transform.position = Vector2.MoveTowards(..." So perhaps try replacing those with more MovePosition calls.
Thanks! I will give this a shot and see if I can get it working
Your answer
![](https://koobas.hobune.stream/wayback/20220613025157im_/https://answers.unity.com/themes/thub/images/avi.jpg)