Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by johnjackperry · Feb 14, 2021 at 10:43 PM · rigidbody2ddynamicrigidbody physics

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


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image johnjackperry · Feb 15, 2021 at 08:40 PM 0
Share

Thanks! I will give this a shot and see if I can get it working

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make collisions where both objects are moving but pass through each other? 0 Answers

Objects moving despite everything being removed 0 Answers

Rigidbody2D constraints checked for x and y but still moves when character jumps on it from distance 0 Answers

How do I prevent a Kinematic Rigidbody2D from moving a Dynamic Rigidbody2D on Collision? 1 Answer

Keeping momentum with rigid bodies. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges