private BuildPhysicsWorld buildPhysicsWorld;
private CollisionWorld collisionWorld;
protected override void OnStartRunning()
{
buildPhysicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>();
collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld;
}
protected override void OnUpdate()
{
float xValue = Input.GetAxis("Horizontal");
float zValue = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(xValue, 0, zValue);
bool isInputAvaliable = direction.magnitude > 0 ? true : false;
Entities.ForEach(( ref Translation translation ,ref MovementComponent movementComponent, ref PhysicsVelocity physicsVelocity, in TimeComponent timeComponent) =>
{
RaycastInput raycastInput = new RaycastInput
{
Start = translation.Value,
End = translation.Value - new float3(0, -1, 0)
};
bool isGrounded = IsGrounded(raycastInput);
if (isInputAvaliable && isGrounded)
{
movementComponent.lastDirection = direction;
physicsVelocity.Linear = new float3(xValue, 0, zValue) * timeComponent.localTimeScale * movementComponent.groundSpeed;
}
}).Run();
}
public bool IsGrounded(RaycastInput raycastInput)
{
Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
if (collisionWorld.CastRay(raycastInput, out raycastHit))
{
return true;
}
return false;
}
this code isn’t working because of Isgrounded function. How should I do that ?