I’ve been porting over a script to ecs that handles some rudimentary pathfinding. Everything worked great until I switched to the new Unity physics package. The ray-casts are firing and colliding; however, they’re behaving unexpectedly. (i.e. hitting when they shouldn’t be, and not hitting when they should).
I’d typically do a Debug.DrawRay to ensure they’re aimed how I want. Is there an equivalent I could use?
Here’s a trimmed down version of the code, there may be an issue I’m not picking up on.
public class PathfindingSystem : JobComponentSystem {
BuildPhysicsWorld physicsWorld;
protected override void OnCreate() {
physicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
}
[BurstCompile]
struct PathfindingSystemJob : IJobForEach<Translation, PathfindingComponent, Rotation, LocalToWorld> {
[ReadOnly] public float deltaTime;
[ReadOnly] public CollisionWorld collisionWorld;
public void Execute(ref Translation translation, ref PathfindingComponent pathfindingComponent, ref Rotation rotation, ref LocalToWorld localToWorld) {
RaycastInput up = new RaycastInput() {
Start = translation.Value + localToWorld.Up * pathfindingComponent.rayCastOffset,
End = localToWorld.Forward * pathfindingComponent.detectionDistance,
Filter = CollisionFilter.Default
};
var offset = Vector3.zero;
// Emit raycasts
if (collisionWorld.CastRay(up)) {
offset -= Vector3.up;
//Debug.Log("hit up");
}
if (offset != Vector3.zero) {
// Avoid obstacle
} else {
// No obstacle, turn towards target
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDependencies) {
var job = new PathfindingSystemJob();
job.deltaTime = Time.deltaTime;
job.collisionWorld = physicsWorld.PhysicsWorld.CollisionWorld;
return job.Schedule(this, inputDependencies);
}
}
@Rory_Havok right. Why you using Direction in End if it’s even named as point And if you look at sources you’ll see it converts it to Direction, and comments describes that Start nad End is points
// The input to ray cast queries consists of
// the Start & End positions of a line segment,
// and a CollisionFilter used to cull potential hits
public struct RaycastInput
{
public float3 Start
{
get => Ray.Origin;
set
{
float3 end = Ray.Origin + Ray.Displacement;
Ray.Origin = value;
Ray.Displacement = end - value;
}
}
public float3 End
{
get => Ray.Origin + Ray.Displacement;
set => Ray.Displacement = value - Ray.Origin;
}
public CollisionFilter Filter;
internal Ray Ray;
}