Skip to content

store hit GameObject in raycast output #4111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.ml-agents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to

### 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
Expand Down
11 changes: 9 additions & 2 deletions com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ public struct RayOutput
/// </summary>
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.
Expand Down Expand Up @@ -334,7 +340,7 @@ public void Update()
}

/// <inheritdoc/>
public void Reset() { }
public void Reset() {}

/// <inheritdoc/>
public int[] GetObservationShape()
Expand Down Expand Up @@ -456,7 +462,8 @@ out DebugDisplayInfo.RayInfo debugRayOut
HasHit = castHit,
HitFraction = hitFraction,
HitTaggedObject = false,
HitTagIndex = -1
HitTagIndex = -1,
HitGameObject = hitObject
};

if (castHit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public class RayPerception3DTests
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.
Expand All @@ -44,18 +54,22 @@ void SetupScene()
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(0, 0, 10);
cube.tag = k_CubeTag;
cube.name = "cube";

var sphere1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere1.transform.position = new Vector3(-5, 0, 5);
sphere1.tag = k_SphereTag;
sphere1.name = "sphere1";

var sphere2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere2.transform.position = new Vector3(5, 0, 5);
// No tag for sphere2
sphere2.name = "sphere2";

var sphere3 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere3.transform.position = new Vector3(0, 0, -10);
sphere3.tag = k_SphereTag;
sphere3.name = "sphere3";

Physics.SyncTransforms();
}
Expand Down Expand Up @@ -296,5 +310,33 @@ public void TestRayZeroLength()
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);
}
}
}
}