Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Roller ball not working [SOLVED]

Discussion in 'ML-Agents' started by harpingseal, Aug 28, 2022.

  1. harpingseal

    harpingseal

    Joined:
    Sep 3, 2020
    Posts:
    57
    Hi so I've just made a rollerball agent excactly as in the example however, It does not work even when I setted the mode to Heuristic and I've already added a Heuristic function, heres my code, nothing's in the output. I am using a brand new project on URP as a template version

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.MLAgents;
    5. using Unity.MLAgents.Sensors;
    6. using Unity.MLAgents.Actuators;
    7.  
    8. public class BallAgent : Agent
    9. {
    10.     // Start is called before the first frame update
    11.     Rigidbody rBody;
    12.     void Start()
    13.     {
    14.         rBody = GetComponent<Rigidbody>();
    15.         rBody.sleepThreshold = 0.0f;
    16.     }
    17.     public Transform Target;
    18.     public float forceMultiplier = 10;
    19.     public override void OnEpisodeBegin()
    20.     {
    21.         // If the Agent fell, zero its momentum
    22.         if (this.transform.localPosition.y < 0)
    23.         {
    24.             this.rBody.angularVelocity = Vector3.zero;
    25.             this.rBody.velocity = Vector3.zero;
    26.             this.transform.localPosition = new Vector3(0, 0.5f, 0);
    27.         }
    28.  
    29.         // Move the target to a new spot
    30.         Target.localPosition = new Vector3(Random.value * 8 - 4,
    31.                                            0.5f,
    32.                                            Random.value * 8 - 4);
    33.     }
    34.     public override void Heuristic(in ActionBuffers actionsOut)
    35.     {
    36.         var continuousActionsOut = actionsOut.ContinuousActions;
    37.         continuousActionsOut[0] = Input.GetAxis("Horizontal");
    38.         continuousActionsOut[1] = Input.GetAxis("Vertical");
    39.     }
    40.     public override void CollectObservations(VectorSensor sensor)
    41.     {
    42.         // Target and Agent positions
    43.         sensor.AddObservation(Target.localPosition);
    44.         sensor.AddObservation(this.transform.localPosition);
    45.  
    46.         // Agent velocity
    47.         sensor.AddObservation(rBody.velocity.x);
    48.         sensor.AddObservation(rBody.velocity.z);
    49.     }
    50.     public override void OnActionReceived(ActionBuffers actionBuffers)
    51.     {
    52.         // Actions, size = 2
    53.         Vector3 controlSignal = Vector3.zero;
    54.         controlSignal.x = actionBuffers.ContinuousActions[0];
    55.         controlSignal.z = actionBuffers.ContinuousActions[1];
    56.         rBody.AddForce(controlSignal * forceMultiplier);
    57.         Debug.Log(controlSignal);
    58.         // Rewards
    59.         float distanceToTarget = Vector3.Distance(this.transform.localPosition, Target.localPosition);
    60.  
    61.         // Reached target
    62.         if (distanceToTarget < 1.42f)
    63.         {
    64.             SetReward(1.0f);
    65.             Debug.Log("REACHED");
    66.             EndEpisode();
    67.         }
    68.  
    69.         // Fell off platform
    70.         else if (this.transform.localPosition.y < 0)
    71.         {
    72.             Debug.Log("FALLED");
    73.             EndEpisode();
    74.         }
    75.     }
    76. }
    77.  
     
  2. harpingseal

    harpingseal

    Joined:
    Sep 3, 2020
    Posts:
    57
    For any one here having proble, just add a desicion requester!