i need to add ray perception sensor 2d to 2d agent on unity, here's my…
- User
i need to add ray perception sensor 2d to 2d agent on unity, here's my PlayerController.cs:
using UnityEngine; using Unity.MLAgents; using Unity.MLAgents.Actuators; using Unity.MLAgents.Sensors; using Unity.MLAgents.Sensors.Reflection;
[AddComponentMenu("")] public class PlayerController : Agent { public float speed = 5f;
code - Assistant
To add a Ray Perception Sensor 2D to your 2D agent, please follow these steps:
- Add the necessary using directive to your code:
csharpusing Unity.MLAgents.Sensors;- Declare a serialized field for the Ray Perception Sensor parameters and the required initialization in the
Start()method.
csharp[SerializeField] private RayPerceptionSensorComponent2D rayPerceptionSensor2D; private void Start() { rb = GetComponent<Rigidbody2D>(); initialPosition = transform.position; // Initialize Ray Perception Sensor. if (rayPerceptionSensor2D == null) { rayPerceptionSensor2D = gameObject.AddComponent<RayPerceptionSensorComponent2D>(); } } - User
i still get this error:
Fewer observations (0) made than vector observation size (42). The observations will be padded. UnityEngine.Debug:LogWarningFormat (string,object[]) Unity.MLAgents.Sensors.VectorSensor:Write (Unity.MLAgents.Sensors.ObservationWriter) (at C:/Projects/Unity/ml-agents-release_20/com.unity.ml-agents/Runtime/Sensors/VectorSensor.cs:56) Unity.MLAgents.Policies.HeuristicPolicy:StepSensors (System.Collections.Generic.List
1<Unity.MLAgents.Sensors.ISensor>) (at C:/Projects/Unity/ml-agents-release_20/com.unity.ml-agents/Runtime/Policies/HeuristicPolicy.cs:133) Unity.MLAgents.Policies.HeuristicPolicy:RequestDecision (Unity.MLAgents.AgentInfo,System.Collections.Generic.List1<Unity.MLAgents.Sensors.ISensor>) (at C:/Projects/Unity/ml-agents-release_20/com.unity.ml-agents/Runtime/Policies/HeuristicPolicy.cs:38) Unity.MLAgents.Agent:SendInfoToBrain () (at C:/Projects/Unity/ml-agents-release_20/com.unity.ml-agents/Runtime/Agent.cs:1130) Unity.MLAgents.Agent:SendInfo () (at C:/Projects/Unity/ml-agents-release_20/com.unity.ml-agents/Runtime/Agent.cs:1368) Unity.MLAgents.Academy:EnvironmentStep () (at C:/Projects/Unity/ml-agents-release_20/com.unity.ml-agents/Runtime/Academy.cs:592) Unity.MLAgents.AcademyFixedUpdateStepper:FixedUpdate () (at C:/Projects/Unity/ml-agents-release_20/com.unity.ml-agents/Runtime/Academy.cs:43) - Assistant
The error occurs because the agent expects 42 observations, but it currently receives 0 observations. This is because the custom CollectObservations method is not collecting any observations. To fix this issue:
- Remove the current CollectObservations method:
csharppublic override void CollectObservations(VectorSensor sensor) { // The observations from ray-based sensor will be automatically collected }- Add a new CollectObservations method using the RayPerceptionSensor:
csharp
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.