private static void ResolveOverlapWithPlayer(Contexts contexts, PlayerEntity player) { Vector3 point1, point2; float radius; var controller = player.characterContoller.Value; if (controller.GetCurrentControllerType() != CharacterControllerType.UnityCharacterController) { return; } var characterController = controller.RealValue as CharacterController; if (characterController == null) { return; } var height = controller.height; radius = controller.radius; var center = controller.center; PhysicsCastHelper.GetCapsule(controller.transform.position, controller.transform.rotation, height, radius, center, controller.direction, out point1, out point2); var colliders = PhysicsCastHelper.CollidersArray; var hits = PhysicsCastHelper.CapsuleOverlap( point1, point2, radius, colliders, UnityLayerManager.GetLayerMask(EUnityLayerName.Player), QueryTriggerInteraction.Ignore, collider => { if (collider == controller.GetCollider() || !(collider is CharacterController)) { return(true); } return(false); }); // calculate penetration for (int i = 0; i < hits; ++i) { Vector3 resolutionDirection = Vector3.up; float resolutionDistance = 0f; Transform overlappedTransform = colliders[i].GetComponent <Transform>(); if (Physics.ComputePenetration(controller.GetCollider(), controller.transform.position, controller.transform.rotation, colliders[i], overlappedTransform.position, overlappedTransform.rotation, out resolutionDirection, out resolutionDistance )) { characterController.Move(resolutionDirection * resolutionDistance); //_logger.InfoFormat("resolutionDirection:{0}, resolutionDistance:{1}, hitCollider:{2}", resolutionDirection, resolutionDistance, overlappedTransform.name); //DebugDraw.DebugArrow(controller.transform.position, resolutionDirection, Color.magenta, 20f, false); } } }
private static void ResolveOverlapWithPlayer(Contexts contexts, PlayerEntity player) { Vector3 point1, point2; float radius; var controller = player.characterContoller.Value; if (controller.GetCurrentControllerType() != CharacterControllerType.UnityCharacterController) { return; } var characterController = controller.RealValue as CharacterController; if (characterController == null) { return; } var height = controller.height; radius = controller.radius; var center = controller.center; PhysicsCastHelper.GetCapsule(controller.transform.position, controller.transform.rotation, height, radius, center, controller.direction, out point1, out point2); var colliders = PhysicsCastHelper.CollidersArray; var hits = PhysicsCastHelper.CapsuleOverlap( point1, point2, radius, colliders, UnityLayerManager.GetLayerMask(EUnityLayerName.Player), QueryTriggerInteraction.Ignore, collider => { //和角色做分离,趴下不需要,自带分离 if (collider == controller.GetCollider() || !(collider is CapsuleCollider && (collider as CapsuleCollider).direction == 1)) { return(true); } return(false); }); int MaxStep = 1; // calculate penetration for (int i = 0; i < hits; ++i) { Vector3 resolutionDirection = Vector3.up; float resolutionDistance = 0f; Transform overlappedTransform = colliders[i].GetComponent <Transform>(); for (int j = 0; j < MaxStep; ++j) { if (Physics.ComputePenetration(characterController, characterController.transform.position, characterController.transform.rotation, colliders[i], colliders[i].transform.position, colliders[i].transform.rotation, out resolutionDirection, out resolutionDistance )) { characterController.Move(new Vector3(0, -0.01f, 0)); _logger.DebugFormat( "ResolveOverlapWithPlayer ResolveOverlapWithPlayer resolutionDirection:{0}, resolutionDistance:{1}, hitCollider:{2}", resolutionDirection.ToStringExt(), resolutionDistance, overlappedTransform.name); } else { break; } } } }