Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug When applying addrelativeforce on rigidbody, rigidbody gets pushed but it keeps bouncing

Discussion in 'Physics' started by Capzeze, Nov 19, 2022.

  1. Capzeze

    Capzeze

    Joined:
    Mar 28, 2021
    Posts:
    3
    Hey, I'm making a little simulation with the idea to add very basic machine learning. There are 2 robots: red and blue robots. These robots have different modes, one of them is meleeMode. In meleeMode they can attack eachother from a very close distance. The attack itself should push the robot backwards with "AddRelativeForce(vector3.back * bounceback, forceMode.impulse)" and the robot does go backwards, but it keeps bouncing upwards. I tried using physics materials to fix this, but it didn't work. Does anyone know how to fix this?

    Code (CSharp):
    1.     private void OnCollisionEnter(Collision collision)
    2.     {
    3.         if (collision.gameObject == otherCube && mode == Modes.melee)
    4.         {
    5.             Debug.Log(collision.gameObject.name + " bang");
    6.             collision.gameObject.GetComponent<Nav>().happiness -= 4;
    7.             if (red)
    8.                 redHappinessAvg -= 4;
    9.             else
    10.                 blueHappinessAvg -= 4;
    11.             //cube.enabled = false;
    12.             cubeRb.AddRelativeForce(Vector3.back * bounceBack, ForceMode.Impulse);
    13.         }
    14.     }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,589
    This is the Editor forum, your post isn't related to the Editor. I'll move it to the correct Physics forum.