diff --git a/com.unity.ml-agents.extensions/Editor/RigidBodySensorComponentEditor.cs b/com.unity.ml-agents.extensions/Editor/RigidBodySensorComponentEditor.cs index 79a17cf4ef..f1606595fb 100644 --- a/com.unity.ml-agents.extensions/Editor/RigidBodySensorComponentEditor.cs +++ b/com.unity.ml-agents.extensions/Editor/RigidBodySensorComponentEditor.cs @@ -16,6 +16,16 @@ public override void OnInspectorGUI() so.Update(); var rbSensorComp = so.targetObject as RigidBodySensorComponent; + if (rbSensorComp.IsTrivial()) + { + EditorGUILayout.HelpBox( + "The Root Body has no Joints, and the Virtual Root is null or the same as the " + + "Root Body's GameObject. This will not generate any useful observations; they will always " + + "be the identity values. Consider removing this component since it won't help the Agent.", + MessageType.Warning + ); + } + bool requireExtractorUpdate; EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties()); diff --git a/com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodySensorComponent.cs b/com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodySensorComponent.cs index e201125bff..a6dad23b66 100644 --- a/com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodySensorComponent.cs +++ b/com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodySensorComponent.cs @@ -91,6 +91,24 @@ internal void SetPoseEnabled(int index, bool enabled) { GetPoseExtractor().SetPoseEnabled(index, enabled); } + + internal bool IsTrivial() + { + if (ReferenceEquals(RootBody, null)) + { + // It *is* trivial, but this will happen when the sensor is being set up, so don't warn then. + return false; + } + var joints = RootBody.GetComponentsInChildren(); + if (joints.Length == 0) + { + if (ReferenceEquals(VirtualRoot, null) || ReferenceEquals(VirtualRoot, RootBody.gameObject)) + { + return true; + } + } + return false; + } } } diff --git a/com.unity.ml-agents.extensions/Tests/Runtime/Sensors/RigidBodySensorTests.cs b/com.unity.ml-agents.extensions/Tests/Runtime/Sensors/RigidBodySensorTests.cs index 2e1a6fb81b..28fbe95e74 100644 --- a/com.unity.ml-agents.extensions/Tests/Runtime/Sensors/RigidBodySensorTests.cs +++ b/com.unity.ml-agents.extensions/Tests/Runtime/Sensors/RigidBodySensorTests.cs @@ -31,6 +31,7 @@ public void TestNullRootBody() var gameObj = new GameObject(); var sensorComponent = gameObj.AddComponent(); + Assert.IsFalse(sensorComponent.IsTrivial()); var sensor = sensorComponent.CreateSensors()[0]; SensorTestHelper.CompareObservation(sensor, new float[0]); } @@ -48,6 +49,7 @@ public void TestSingleRigidbody() UseLocalSpaceTranslations = true, UseLocalSpaceRotations = true }; + Assert.IsTrue(sensorComponent.IsTrivial()); var sensor = sensorComponent.CreateSensors()[0]; sensor.Update(); @@ -93,6 +95,7 @@ public void TestBodiesWithJoint() UseLocalSpaceLinearVelocity = true }; sensorComponent.VirtualRoot = virtualRoot; + Assert.IsFalse(sensorComponent.IsTrivial()); var sensor = sensorComponent.CreateSensors()[0]; sensor.Update(); diff --git a/com.unity.ml-agents/CHANGELOG.md b/com.unity.ml-agents/CHANGELOG.md index 6c5be75aa8..b11a00b1f3 100755 --- a/com.unity.ml-agents/CHANGELOG.md +++ b/com.unity.ml-agents/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to #### ml-agents / ml-agents-envs / gym-unity (Python) - Added a fully connected visual encoder for environments with very small image inputs. (#5351) ### Bug Fixes +- RigidBodySensorComponent now displays a warning if it's used in a way that won't generate useful observations. (#5387) ## [2.0.0-exp.1] - 2021-04-22