浏览代码

store hit GameObject in raycast output (#4111)

* store hit GameObject in raycast output

* changelog

* fix unit tests

* formatting
/MLA-1734-demo-provider
GitHub 5 年前
当前提交
4efb0119
共有 3 个文件被更改,包括 52 次插入2 次删除
  1. 1
      com.unity.ml-agents/CHANGELOG.md
  2. 11
      com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs
  3. 42
      com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs

1
com.unity.ml-agents/CHANGELOG.md


### Minor Changes
#### com.unity.ml-agents (C#)
- `RayPerceptionSensor.Perceive()` now additionally store the GameObject that was hit by the ray. (#4111)
#### ml-agents / ml-agents-envs / gym-unity (Python)
### Bug Fixes

11
com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs


public float HitFraction;
/// <summary>
/// The hit GameObject (or null if there was no hit).
/// </summary>
public GameObject HitGameObject;
/// <summary>
/// Writes the ray output information to a subset of the float array. Each element in the rayAngles array
/// determines a sublist of data to the observation. The sublist contains the observation data for a single cast.
/// The list is composed of the following:

}
/// <inheritdoc/>
public void Reset() { }
public void Reset() {}
/// <inheritdoc/>
public int[] GetObservationShape()

HasHit = castHit,
HitFraction = hitFraction,
HitTaggedObject = false,
HitTagIndex = -1
HitTagIndex = -1,
HitGameObject = hitObject
};
if (castHit)

42
com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs


const string k_CubeTag = "Player";
const string k_SphereTag = "Respawn";
[TearDown]
public void RemoveGameObjects()
{
var objects = GameObject.FindObjectsOfType<GameObject>();
foreach (var o in objects)
{
UnityEngine.Object.DestroyImmediate(o);
}
}
void SetupScene()
{
/* Creates game objects in the world for testing.

var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(0, 0, 10);
cube.tag = k_CubeTag;
cube.name = "cube";
sphere1.name = "sphere1";
sphere2.name = "sphere2";
sphere3.name = "sphere3";
Physics.SyncTransforms();
}

// hit fraction is arbitrary but should be finite in [0,1]
Assert.GreaterOrEqual(outputBuffer[2], 0.0f);
Assert.LessOrEqual(outputBuffer[2], 1.0f);
}
}
[Test]
public void TestStaticPerceive()
{
SetupScene();
var obj = new GameObject("agent");
var perception = obj.AddComponent<RayPerceptionSensorComponent3D>();
perception.RaysPerDirection = 0; // single ray
perception.MaxRayDegrees = 45;
perception.RayLength = 20;
perception.DetectableTags = new List<string>();
perception.DetectableTags.Add(k_CubeTag);
perception.DetectableTags.Add(k_SphereTag);
var radii = new[] { 0f, .5f };
foreach (var castRadius in radii)
{
perception.SphereCastRadius = castRadius;
var castInput = perception.GetRayPerceptionInput();
var castOutput = RayPerceptionSensor.Perceive(castInput);
Assert.AreEqual(1, castOutput.RayOutputs.Length);
// Expected to hit the cube
Assert.AreEqual("cube", castOutput.RayOutputs[0].HitGameObject.name);
}
}
}
正在加载...
取消
保存