public bool CheckCollision(PECapsuleTrigger other, out PECapsuleHitResult result) { int count1 = Mathf.FloorToInt(0.5f * moveDir1.magnitude / scaledRadius) + 1; int count2 = Mathf.FloorToInt(0.5f * moveDir2.magnitude / scaledRadius) + 1; //Vector3 normalizedMoveDir1 = moveDir1.normalized; //Vector3 normalizedMoveDir2 = moveDir2.normalized; float radius11 = Vector3.Distance(m_LastCenterPos1, m_ParentCenterPos); float radius12 = Vector3.Distance(m_CenterPos1, m_ParentCenterPos); float radius21 = Vector3.Distance(m_LastCenterPos2, m_ParentCenterPos); float radius22 = Vector3.Distance(m_CenterPos2, m_ParentCenterPos); int count = Mathf.Max(count1, count2); float pCount = count > 1 ? count - 1 : count; for (int i = 0; i < count; ++i) { float p = i / pCount; Vector3 checkPoint1 = Vector3.Lerp(m_LastCenterPos1, m_CenterPos1, p); Vector3 checkPoint2 = Vector3.Lerp(m_LastCenterPos2, m_CenterPos2, p); if (m_ParentCenterPos != Vector3.zero) { if (checkPoint1 != m_ParentCenterPos) { checkPoint1 = m_ParentCenterPos + (checkPoint1 - m_ParentCenterPos).normalized * Mathf.Lerp(radius11, radius12, p); } if (checkPoint2 != m_ParentCenterPos) { checkPoint2 = m_ParentCenterPos + (checkPoint2 - m_ParentCenterPos).normalized * Mathf.Lerp(radius21, radius22, p); } } if (CheckCollision(checkPoint1, checkPoint2, other, out result)) { return(true); } } result = null; return(false); }
bool CheckCollision(Vector3 pos1, Vector3 pos2, PECapsuleTrigger other, out PECapsuleHitResult result) { Vector3 closestPosSelf; Vector3 closestPosOther; WhiteCat.Utility.ClosestPoint(pos1, pos2, other.m_CenterPos1, other.m_CenterPos2, out closestPosSelf, out closestPosOther); Vector3 dir = closestPosOther - closestPosSelf; float radiusSum = scaledRadius + other.scaledRadius; if (dir.sqrMagnitude < radiusSum * radiusSum) { result = new PECapsuleHitResult(); result.selfTrans = trans; result.hitTrans = other.trans; result.hitPos = closestPosSelf + dir * scaledRadius / radiusSum; result.hitDir = (moveDir1 + moveDir2 + dir).normalized; result.distance = 0; return(true); } result = null; return(false); }