1

I am trying to override the OnActionReceived method of the Agent class. But I don't think that the function is getting called and it doesn't give me any error.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;

public class MoveCar : Agent
{
    // more code here

    public override void OnActionReceived(ActionBuffers actions)
    {
        // Get the acceleration, brake, and steer actions from the ML-Agents system
        float acceleration = actions.ContinuousActions[0];
        float brake = Mathf.Clamp(actions.ContinuousActions[1], 0, 1);
        float steer = actions.ContinuousActions[2];

        Debug.Log(acceleration);


        nowAcceleration = motorMax * acceleration;
        nowBrake = brakeMax * brake;
        nowSteer = steerMax * steer;

        // Apply the actions to the car
        wheelBackLeft.motorTorque = nowAcceleration;
        wheelBackRight.motorTorque = nowAcceleration;

        wheelBackLeft.brakeTorque = nowBrake;
        wheelBackRight.brakeTorque = nowBrake;
        // wheelFrontLeft.brakeTorque = nowBrake;
        // wheelFrontRight.brakeTorque = nowBrake;

        wheelFrontLeft.steerAngle = nowSteer;
        wheelFrontRight.steerAngle = nowSteer;

        updateWheel(wheelFrontLeft, frontLeftTransform);
        updateWheel(wheelFrontRight, frontRightTransform);
        updateWheel(wheelBackLeft, backLeftTransform);
        updateWheel(wheelBackRight, backRightTransform);
    }

    // more code here
}

I wanted the car to move and to see my acceleration in the Unity3d console but the car does nothing and nothing is printed in the console.

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
  • Nice, where did you override the method? Please provide more context. – Pinke Helga Dec 29 '22 at 23:27
  • this is my full code: https://sourceb.in/78lxody1xB – MaCHeaMaRoBi Dec 30 '22 at 09:13
  • I'm not too familiar with Unity3d's vehicle mechanics anymore. However, It could help supporters to rule out implementation errors if you described as well how you've setup the class in the scene. You can edit your question with the link below the Q. – Pinke Helga Dec 30 '22 at 13:41
  • @MaCHeaMaRoBi question is already asked [here](https://stackoverflow.com/questions/70636584/unity-with-ml-agents-onactionreceived-is-always-empty) – zain ul din Dec 30 '22 at 13:41
  • Does this answer your question? [Trying to use OnActionReceived(ActionBuffers action) with MLAgents for Unity](https://stackoverflow.com/questions/70238772/trying-to-use-onactionreceivedactionbuffers-action-with-mlagents-for-unity) – zain ul din Dec 30 '22 at 13:42

0 Answers0