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);
}
}
}
}