Hi all,
I’d be grateful for some feedback on how to set up tests when they perform ray casts using Unity Physics. Sadly no tests I can copy since the Physics tests I’ve seen are unit tests. I’ve read the Physics source, but am still unsure why in the tests, the colliders are not hit by raycasts.
In this specific case, I am trying to find the closest enemy that is hit by a raycast (selecting an enemy to aim at).
The test:
SelectAimSystemTest.cs
[TestFixture]
public class SelectAimSystemTest : BaseTest<SelectAimSystem>
{
[Test]
public void selectsClosestTargetTest()
{
var aimingEntity = em.CreateEntity(
typeof(Translation),
typeof(Rotation),
typeof(LocalToWorld),
typeof(FactionComponent),
typeof(Aiming),
typeof(PhysicsVelocity),
typeof(PhysicsCollider)
);
var aimingTranslation = em.
GetComponentData<Translation>(
aimingEntity
).
Value;
em.SetComponentData(
aimingEntity,
new FactionComponent
{
Value = (byte) Factions.Id.Allies
}
);
em.SetComponentData(
aimingEntity,
new LocalToWorld
{
Value = new float4x4(
quaternion.identity,
aimingTranslation
)
}
);
em.SetComponentData(
aimingEntity,
new PhysicsCollider
{
Value = SphereCollider.Create(
new SphereGeometry
{
Center = aimingTranslation,
Radius = 0.5f
}
)
}
);
var targetEntity1 = em.CreateEntity(
typeof(Translation),
typeof(Rotation),
typeof(LocalToWorld),
typeof(FactionComponent),
typeof(PhysicsVelocity),
typeof(PhysicsCollider)
);
var targetTranslation1 = new float3(10, 0, 10);
em.SetComponentData(
targetEntity1,
new Translation
{
Value = targetTranslation1
}
);
em.SetComponentData(
targetEntity1,
new FactionComponent
{
Value = (byte) Factions.Id.Axis
}
);
em.SetComponentData(
targetEntity1,
new LocalToWorld
{
Value = new float4x4(
quaternion.identity,
targetTranslation1
)
}
);
em.SetComponentData(
targetEntity1,
new PhysicsCollider
{
Value = SphereCollider.Create(
new SphereGeometry
{
Center = em.
GetComponentData<Translation>(
targetEntity1
).
Value,
Radius = 0.5f
}
)
}
);
var aiming = em.GetComponentData<Aiming>(aimingEntity);
Assert.AreEqual(new float3(0, 0, 0), aiming.globalDirection);
World.Update();
aiming = em.GetComponentData<Aiming>(aimingEntity);
Assert.AreEqual(new float3(10, 0, 10), aiming.globalDirection);
}
}
Aiming is the component which should contain the result direction where the enemy is. Faction determines who is a valid target. Adding a PhysicsCollider for the raycast.
The test base class sets up the basic systems:
BaseTest.cs
public class BaseTest<T> : ECSTestsFixture where T : ComponentSystemBase
{
protected EntityManager em;
[SetUp]
public override void Setup()
{
base.Setup();
em = m_Manager;
var systems = new List<Type>
{
// Defaults are added in DefaultWorldInitialization.
// var initializationSystemGroup = world.GetOrCreateSystem<InitializationSystemGroup>();
// var simulationSystemGroup = world.GetOrCreateSystem<SimulationSystemGroup>();
// var presentationSystemGroup = world.GetOrCreateSystem<PresentationSystemGroup>();
typeof(BeginInitializationEntityCommandBufferSystem),
typeof(EndInitializationEntityCommandBufferSystem),
typeof(BeginFixedStepSimulationEntityCommandBufferSystem),
typeof(FixedStepSimulationSystemGroup),
typeof(EndFixedStepSimulationEntityCommandBufferSystem),
typeof(BeginSimulationEntityCommandBufferSystem),
typeof(EndSimulationEntityCommandBufferSystem),
typeof(LateSimulationSystemGroup),
typeof(BeginPresentationEntityCommandBufferSystem),
typeof(BuildPhysicsWorld),
typeof(StepPhysicsWorld),
typeof(ExportPhysicsWorld),
typeof(EndFramePhysicsSystem),
typeof(ServerPerformPhysicsGroup),
typeof(T),
};
DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(World, systems);
World.SetTime(new TimeData(1.234, 0.016f));
}
}
Has anyone set up tests to test raycasts in their systems and can give me some pointers by any chance?
P.S: The SelectAimSystem runs after the BuildPhysicsWorld system and before the StepPhysicsWorld system.