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

Question Trained Agent always returns to scene origin

Discussion in 'ML-Agents' started by donald212, Mar 15, 2023.

  1. donald212

    donald212

    Joined:
    Feb 22, 2020
    Posts:
    2
    Hi,
    I am trying to train Agent (enemy) to chase the player. In training scenarios, it works well but when I put Agent in a real environment he always moves to the origin of the scene.

    I have 9 training scenarios like this (player, finish, ground, obstacles and enemy). The player moves towards the finish (green trigger) and the enemy is chasing him. I am using Ray Perception Sensor 3D to see the player and 1 discrete branch of size 7 to control the movement and rotation of the enemy (same principle as shown in some examples).

    1.png

    In my OnActionReceived method, I call MoveAgeng with discrete actions (as shown in the code below) and then I check if the distance to the player is below some threshold to AddReward and end the episode.

    Code (CSharp):
    1. public void MoveAgent(ActionSegment<int> act)
    2.         {
    3.             var direction = Vector3.zero;
    4.             var rotation = Vector3.zero;
    5.  
    6.             var action = act[0];
    7.  
    8.             switch (action)
    9.             {
    10.                 case 1:
    11.                     direction = transform.forward * 1f;
    12.                     break;
    13.                 case 2:
    14.                     direction = transform.forward * -1f;
    15.                     break;
    16.                 case 3:
    17.                     rotation = transform.up * 1f;
    18.                     break;
    19.                 case 4:
    20.                     rotation = transform.up * -1f;
    21.                     break;
    22.                 case 5:
    23.                     direction = transform.right * -0.75f;
    24.                     break;
    25.                 case 6:
    26.                     direction = transform.right * 0.75f;
    27.                     break;
    28.             }
    29.  
    30.             transform.Rotate(rotation, Time.fixedDeltaTime * rotationSpeed, Space.World);
    31.             var move = body.Speed * Time.fixedDeltaTime * direction;
    32.             transform.Translate(move, Space.World);
    33.         }
    I tried also some other ways to move the agent (rigidbody AddForce, MovePosition, +=, changing Space.World to Space.Self etc.). I was able to train agent with these variants of movement but they act the same in my main scene (real environment). There is some problem with Local vs World space coordinates but I can not figure it out. act[0] returns the direction to the origin.

    I put my agent to scene root e.g. (240, 0, -65) with a trained brain attached (or with mlagents-learn active) and he runs to (0, 0, 0). I also tried to parent him to empty gameobject placed on (240, 0, -65) so the agent itself was on (0, 0, 0) relative to his parent but this time he runs to (-240, 0, 65). Same place but with different parenting.

    It is strange also because only one scenario is in world position (0, 0, 0) and in these scenarios agent always begins in (0, 0, 0) relative to this scenario.

    I would be glad for any help. Thanks.

    Mlagents release 19. Unity 2022.2.0b12.
     
  2. donald212

    donald212

    Joined:
    Feb 22, 2020
    Posts:
    2
    The problem was that the Agent learned that the player would be on (0, 0, 0) when he cannot observe him so he came there in world space.