Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

help! stuck with rigidbody behaviour

Discussion in 'ML-Agents' started by AndrewGri, Apr 22, 2020.

  1. AndrewGri


    Jan 30, 2020
    Hi all.

    I'm so tired to fight with rigidbody. I'm using the following code to make my agent run.

    Code (CSharp):
    1.         switch (action)
    2.         {
    3.             case 1:
    4.                 dirToGo = this.transform.forward * 1f;
    5.                 break;
    6.             case 2:
    7.                 dirToGo = this.transform.forward * -1f;
    8.                 break;
    9.             case 3:
    10.                 rotateDir = this.transform.up * 1f;
    11.                 break;
    12.             case 4:
    13.                 rotateDir = this.transform.up * -2f;
    14.                 break;
    15.         }
    16.         this.transform.Rotate(rotateDir, Time.deltaTime * 200f * 2.0f);
    17.         rBody.AddForce(dirToGo * 2f, ForceMode.VelocityChange);
    19.         Vector3 curVelocity = rBody.velocity;
    20.         rBody.velocity = Vector3.ClampMagnitude(curVelocity, maxAgentSpeed);

    My arena is flat and I expect to have Y Axis value as 0.5 for my agent. This the config of the rigidbody.

    But at some point of training, after collision with another object starts to change it's Y axis value.

    I'm trying to return it back to Y=0.5 checking Y value and calling Done() if it is higher than 0.6 or lower than 0.4. In AgentReset() I call the code below.

    Code (CSharp):
    1.         rBody.angularVelocity =;
    2.         rBody.velocity =;
    3.         rBody.Sleep();
    4.         agent.transform.position = new Vector3(curPos.x, 0.5f, curPos.z);
    5.         agent.transform.localPosition = new Vector3(curPos.x, 0.5f, curPos.z);
    but it does not help, the agent starts resetting almost indefinitely and does not want to change Y back to 0.5.

    I check all colliders on the scene, they all correct and intersect the arena by 90 degrees.

    I also tried to Freeze Y position in Rigidbody interface, but in this case my agent stops moving at all.

    I do not have an idea how to fix that. Parsed all Google, but still does not have a solution.
    I still think it is something very silly and answer is simple and probably I just do not understand the logic of rigidbody.
    Anyway if you can give an advice, it will be great.

    Thanks in advance.
  2. TreyK-47


    Unity Technologies

    Oct 22, 2019
    I'll flag this for the team to have a look! Which versions of python & C# are you running?
  3. christophergoy


    Unity Technologies

    Sep 16, 2015
    Is your rigidBody parented by your agent's transform? If so, you are rotating your agent, which will rotate your rigidbody.