- Home /
My code dont work on a MLAgents, Unity 2020.2.7f
Hello, i am having trouble on doing an agent, I Installed everything, use the command prompt to activate the venv the route to activate the unity for agents. This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
public class Movi : Agent
{
[SerializeField] private Transform targetTransform;
public override void OnEpisodeBegin()
{
transform.position = Vector3.zero;
}
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(transform.position);
sensor.AddObservation(targetTransform.position);
}
public override void OnActionReceived(ActionBuffers actions)
{
float moveX = actions.ContinuousActions[0];
float moveZ = actions.ContinuousActions[1];
float moveSpeed = 50f;
transform.position += new Vector3(moveX, 0, moveZ) * Time.deltaTime * moveSpeed;
}
public override void Heuristic(in ActionBuffers actionsOut)
{
ActionSegment<float> continuousActions = actionsOut.ContinuousActions;
continuousActions[0] = Input.GetAxisRaw("Horizontal");
continuousActions[1] = Input.GetAxisRaw("Vertical");
}
private void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent<Goal>(out Goal goal))
{
SetReward(+1f);
EndEpisode();
}
if (other.TryGetComponent<Wall>(out Wall Wall))
{
SetReward(-1f);
EndEpisode();
}
}
}
I am not having any error, just that it dont work, if it can help, I am following this video: https://www.youtube.com/watch?v=zPFU30tbyKs&t=806s Hope someone can help me!
Comment
Your answer
Follow this Question
Related Questions
Help with Putting GameObjects into a list. 2 Answers
Adding collider to 3D model 2 Answers
How easy is Unity to use to create an android game if you know Java and some C#? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers