Hello
I am trying to understand why my character falls though my boxes while it don’t fall through the plane. Two of the boxes have different scales. I feel that its something basic that’s missing or some ECS logic that i am not considering in my physics setup. No errors or warnings so i am kind of lost were i should start debugging. I might suspect the parameter CollidesWith in the RaycastInput, because i don’t understand why i need to select physics layer with bitshifting.
I am thankful for any advise.
public class CharacterControllerSystem : SystemBase
{
private BuildPhysicsWorld buildPhysicsWorldSystem;
protected override void OnCreate()
{
buildPhysicsWorldSystem = World.GetExistingSystem<BuildPhysicsWorld>();
}
protected override void OnUpdate()
{
var rayCaster = new MovementRayCast() { physicsWorld = World.GetExistingSystem<BuildPhysicsWorld>().PhysicsWorld };
float deltaTime = Time.DeltaTime;
Entities.ForEach((ref CharacterDataComponent characterDataComponent, ref Translation translation, ref Rotation rotation, in PlayerControllerComponent playerControllerComponent) =>
{
//Rotation camera independent
if (playerControllerComponent.leftAlt)
{
}
else
{
rotation.Value = quaternion.RotateY(playerControllerComponent.cameraHorizontal);
}
//Driving the gameobject with data from PlayerControllerComponent
float3 vectorRight = math.mul(rotation.Value, math.right());
vectorRight.y = 0;
vectorRight = math.normalizesafe(vectorRight);
translation.Value = translation.Value + (playerControllerComponent.inputVertical * math.forward(rotation.Value)) * 5f * deltaTime;
translation.Value = translation.Value + (playerControllerComponent.inputHorizontal * vectorRight) * 5f * deltaTime;
characterDataComponent.centerPosition = new float3(translation.Value.x, translation.Value.y + characterDataComponent.centerHeight, translation.Value.z);
//Raycasting end position
var rayCastEnd90Pos = new float3(translation.Value.x, -characterDataComponent.centerHeight, translation.Value.z);
var rayCastEndSlopePos = math.mul(rotation.Value, rayCastEnd90Pos);
characterDataComponent.debugPosition = rayCastEnd90Pos; //debug
//jumping TODO add acceleration to jump
if (playerControllerComponent.jump && characterDataComponent.IsGrounded)
{
translation.Value = translation.Value + (-characterDataComponent.gravity) * 50f * deltaTime;
}
//check for ground
if (rayCaster.CheckRay(characterDataComponent.centerPosition, rayCastEnd90Pos, characterDataComponent.centerHeight))
{
characterDataComponent.IsGrounded = true;
}
else
{
translation.Value = translation.Value + characterDataComponent.gravity * deltaTime;
characterDataComponent.IsGrounded = false;
}
//check for Step or slope
}).Schedule();
buildPhysicsWorldSystem.AddInputDependencyToComplete(Dependency);
}
private struct MovementRayCast
{
public PhysicsWorld physicsWorld;
public bool CheckRay(float3 startingPosition, float3 direction, float rayLength)
{
var ray = new RaycastInput()
{
Start = startingPosition,
End = startingPosition + (direction * rayLength),
Filter = new CollisionFilter()
{
GroupIndex = 0,
BelongsTo = 1u << 2,
CollidesWith = 1u << 1
}
};
return physicsWorld.CastRay(ray);
}
}
}
