Hi everyone!
After having searched and learned a lot through these threads, i still can’t realize what i’m doing wrong in this ground check system…
I can’t get my ray hits something.
Here it is the code, very simple:
Code
[UpdateAfter(typeof(ExportPhysicsWorld)), UpdateBefore(typeof(EndFramePhysicsSystem))]
public class GroundCheckSystem : SystemBase
{
BuildPhysicsWorld _BuildPhysicsWorldSystem;
protected override void OnCreate() {
_BuildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
}
protected override void OnUpdate( )
{
Dependency = JobHandle.CombineDependencies(_BuildPhysicsWorldSystem.FinalJobHandle, Dependency);
var collisionWorld = _BuildPhysicsWorldSystem.PhysicsWorld.CollisionWorld;
Entities
.WithReadOnly(collisionWorld)
.ForEach((ref CharacterDataComponent data, in Translation position, in PhysicsCollider collider ) =>
{
RaycastInput input = new RaycastInput()
{
Start = position.Value,
End = position.Value - new float3(0,1.5f,0),
Filter = collider.Value.Value.Filter
};
data.isGrounded = collisionWorld.CastRay(input);
}).ScheduleParallel();
}
}
Any help is really appreciated!
This all seems fine. I’d check a couple of things (sorry if some of them are too silly):
- Check the filter on your collider; you can also try to cast a ray with the default filter to see if anything hits… Also, is your world setup in a way that filters should actually allow some hits?
- Is Y axis your UP axis? Also, is there anything 1.5m or closer to the character along the Y axis? Maybe try to increase it to say 10m and verify.
This is a side note, but did you try to use the CharacterControllerUtilities.CheckSupport()? It’s meant to do exactly what you need.
Hi, thanks you very much for your answer!
I just got it working, but honestly I don’t really know what has done the trick 
My collision filters are fine I think… i’ve set the ground belongs to 0 and player belongs to 1. and both collide with everythings except for their respective layer.
In the code i’ve added some debug draws and edited the foreach loop by adding
Dependency = Entity…ScheduleParallel(Dependency).
Then i’ve also rewritten the RaycastInput in another way using LocalToWorld component instead of Translation. maybe this made it works.
I’m not sure, but now it works and here it is the new code:
Code
[UpdateAfter(typeof(StepPhysicsWorld)), UpdateBefore((typeof(EndFramePhysicsSystem)))]
public class GroundCheckSystem : SystemBase
{
BuildPhysicsWorld m_BuildPhysicsWorldSystem;
protected override void OnCreate()
{
m_BuildPhysicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
}
protected override void OnUpdate()
{
Dependency = JobHandle.CombineDependencies(Dependency, m_BuildPhysicsWorldSystem.FinalJobHandle);
CollisionWorld world = m_BuildPhysicsWorldSystem.PhysicsWorld.CollisionWorld;
Dependency = Entities
.WithReadOnly(world)
.ForEach((ref CharacterDataComponent data, in LocalToWorld t, in PhysicsCollider pc) =>
{
RaycastInput RayInput = new RaycastInput
{
Start = t.Position + t.Up * data.verticalOffset,
End = (t.Position + t.Up * data.verticalOffset) - t.Up * data.rayDistance,
Filter = pc.Value.Value.Filter
};
data.isGrounded = world.CastRay(RayInput, out Unity.Physics.RaycastHit hit);
//Debug
if(data.drawDebugGizmos) {
Debug.DrawRay(RayInput.Start,-t.Up * data.rayDistance, Color.green);
if(data.isGrounded) {
Debug.DrawLine(hit.Position-new float3(.2f,.2f,0), hit.Position+new float3(.2f,.2f,0),Color.red,.05f);
Debug.DrawLine(hit.Position-new float3(.2f,-.2f,0), hit.Position+new float3(.2f,-.2f,0),Color.red,.05f);
}
}
}).ScheduleParallel(Dependency);
}
}
1 Like