private static bool IsHitGround(PlayerEntity playerEntity, float dist) { var gameObject = playerEntity.RootGo(); var prevLayer = gameObject.layer; var prev = IntersectionDetectTool.SetColliderDisable(gameObject); var playerPosition = gameObject.transform.position; var playerRotation = gameObject.transform.rotation; var controller = playerEntity.characterContoller.Value; var valueRadius = playerEntity.characterContoller.Value.radius; var isHit = false; RaycastHit outHit; if (UseSphereCast(controller)) { //use sphere cast var position = playerPosition + playerRotation * new Vector3(0f, valueRadius, 0f); // DebugDraw.DebugWireSphere(startPoint, Color.red, CastRadius, 1f); // DebugDraw.DebugWireSphere(startPoint + new Vector3(0,targetHeight - CastRadius - LiftUp,0), Color.magenta, CastRadius, 1f); isHit = PhysicsCastHelper.SphereCast(position, CastRadius, Vector3.down, out outHit, dist, UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, 0.1f); if (!isHit) { //DebugDraw.DebugWireSphere(position + (isHit ? Vector3.down * outHit.distance : Vector3.down * dist),isHit ? Color.green : (playerEntity.characterContoller.Value.isGrounded ? Color.magenta : Color.red) ,CastRadius, isHit ? 0f:60f); } } else { Vector3 point1, point2; float radius; 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); isHit = PhysicsCastHelper.CapsuleCast(point1, point2, radius, Vector3.down, out outHit, dist, UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, 0.1f); if (!isHit && false) { PhysicsCastHelper.GetDebugDrawTypeCapsule(controller.transform.position, controller.transform.rotation, height, radius, center, controller.direction, out point1, out point2); DebugDraw.DebugCapsule(point1 + (isHit ? Vector3.down * outHit.distance : Vector3.down * dist), point2 + (isHit ? Vector3.down * outHit.distance : Vector3.down * dist), isHit ? Color.green : (playerEntity.characterContoller.Value.isGrounded ? Color.magenta : Color.red), radius, isHit ? 0f:60f ); } } IntersectionDetectTool.RestoreCollider(gameObject, prev); return(isHit); }
private static bool IsHitGround(PlayerEntity playerEntity, float dist) { var gameObject = playerEntity.RootGo(); var prevLayer = gameObject.layer; IntersectionDetectTool.SetColliderDisable(gameObject, IntersectionDetectTool.ColliderEnableState); var playerPosition = gameObject.transform.position; var playerRotation = gameObject.transform.rotation; var controller = playerEntity.characterContoller.Value; var valueRadius = playerEntity.characterContoller.Value.radius; var isHit = false; RaycastHit outHit; if (UseSphereCast(controller)) { //use sphere cast var position = playerPosition + playerRotation * new Vector3(0f, valueRadius, 0f); isHit = PhysicsCastHelper.SphereCast(position, CastRadius, Vector3.down, out outHit, dist, UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, 0.1f); } else { Vector3 point1, point2; float radius; 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); isHit = PhysicsCastHelper.CapsuleCast(point1, point2, radius, Vector3.down, out outHit, dist, UnityLayers.AllCollidableLayerMask, QueryTriggerInteraction.Ignore, 0.1f); } IntersectionDetectTool.RestoreCollider(gameObject, IntersectionDetectTool.ColliderEnableState); return(isHit); }
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; } } } }