fix: 플레이어 접촉 이동과 타깃 표면 추적 보정
- TargetSurfaceUtility를 추가해 플레이어와 적의 실제 충돌 표면 기준으로 거리, 방향, 목적지를 계산 - 플레이어 이동과 적 루트모션, 추적 로직에서 접촉 시 수평 이동을 제한해 겹침과 밀어내기 문제를 완화 - 드로그 AI 거리 판정 노드들이 표면 거리 기준을 사용하도록 맞춰 사거리 분기 오차를 줄임
This commit is contained in:
@@ -92,6 +92,7 @@ namespace Colosseum.Enemy
|
||||
public EnemyData Data => enemyData;
|
||||
public Animator Animator => animator;
|
||||
public bool UseThreatSystem => useThreatSystem;
|
||||
public bool IsTouchingPlayerContact => IsTouchingPlayer();
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
@@ -129,6 +130,7 @@ namespace Colosseum.Enemy
|
||||
|
||||
UpdateThreatState(Time.deltaTime);
|
||||
OnServerUpdate();
|
||||
ApplyPlayerContactMovementLock();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -137,12 +139,13 @@ namespace Colosseum.Enemy
|
||||
protected virtual void OnServerUpdate() { }
|
||||
|
||||
/// <summary>
|
||||
/// 보스와 플레이어가 겹치면 적 자신을 살짝 밀어내 겹침을 해소합니다.
|
||||
/// 점프 착지 포함, 항상 실행됩니다.
|
||||
/// 접촉 잠금 보정을 사용하지 않는 적만 플레이어 겹침 해소를 적용합니다.
|
||||
/// </summary>
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (!IsServer || IsDead) return;
|
||||
if (freezeHorizontalMotionOnPlayerContact)
|
||||
return;
|
||||
|
||||
Vector3 separationOffset = ComputePlayerSeparationOffset();
|
||||
if (separationOffset.sqrMagnitude <= 0.000001f)
|
||||
@@ -202,11 +205,21 @@ namespace Colosseum.Enemy
|
||||
// XZ: 애니메이션 진행도에 따라 목표 위치로 lerp
|
||||
float t = Mathf.Clamp01(animator.GetCurrentAnimatorStateInfo(0).normalizedTime);
|
||||
Vector3 newXZ = Vector3.Lerp(jumpStartXZ, jumpTargetXZ, t);
|
||||
transform.position = new Vector3(newXZ.x, transform.position.y + deltaPosition.y, newXZ.z);
|
||||
Vector3 desiredDelta = new Vector3(
|
||||
newXZ.x - transform.position.x,
|
||||
deltaPosition.y,
|
||||
newXZ.z - transform.position.z);
|
||||
if (freezeHorizontalMotionOnPlayerContact)
|
||||
desiredDelta = LimitHorizontalDeltaAgainstPlayerContact(desiredDelta);
|
||||
|
||||
transform.position += desiredDelta;
|
||||
}
|
||||
else
|
||||
{
|
||||
// jumpToTarget 없으면 기존처럼 애니메이션 루트모션 그대로 적용
|
||||
if (freezeHorizontalMotionOnPlayerContact)
|
||||
deltaPosition = LimitHorizontalDeltaAgainstPlayerContact(deltaPosition);
|
||||
|
||||
transform.position += deltaPosition;
|
||||
}
|
||||
}
|
||||
@@ -218,18 +231,25 @@ namespace Colosseum.Enemy
|
||||
isAirborne = false;
|
||||
if (hasJumpTarget)
|
||||
{
|
||||
// lerp가 1.0에 못 미쳐도 착지 시 정확한 위치로 스냅
|
||||
transform.position = new Vector3(jumpTargetXZ.x, transform.position.y, jumpTargetXZ.z);
|
||||
// lerp가 1.0에 못 미쳐도 착지 시 목표 지점으로 보정하되, 플레이어 표면을 넘지 않도록 제한합니다.
|
||||
Vector3 landingDelta = new Vector3(
|
||||
jumpTargetXZ.x - transform.position.x,
|
||||
0f,
|
||||
jumpTargetXZ.z - transform.position.z);
|
||||
if (freezeHorizontalMotionOnPlayerContact)
|
||||
landingDelta = LimitHorizontalDeltaAgainstPlayerContact(landingDelta);
|
||||
|
||||
transform.position += landingDelta;
|
||||
}
|
||||
hasJumpTarget = false;
|
||||
navMeshAgent.enabled = true;
|
||||
navMeshAgent.Warp(transform.position);
|
||||
}
|
||||
|
||||
if (freezeHorizontalMotionOnPlayerContact && IsTouchingPlayer())
|
||||
if (freezeHorizontalMotionOnPlayerContact)
|
||||
{
|
||||
deltaPosition.x = 0f;
|
||||
deltaPosition.z = 0f;
|
||||
ApplyPlayerContactMovementLock();
|
||||
deltaPosition = LimitHorizontalDeltaAgainstPlayerContact(deltaPosition);
|
||||
}
|
||||
|
||||
navMeshAgent.Move(deltaPosition);
|
||||
@@ -337,7 +357,7 @@ namespace Colosseum.Enemy
|
||||
return Vector3.zero;
|
||||
|
||||
float scanRadius = GetPlayerDetectionRadius();
|
||||
int count = Physics.OverlapSphereNonAlloc(transform.position, scanRadius, overlapBuffer);
|
||||
int count = Physics.OverlapSphereNonAlloc(bodyCollider.bounds.center, scanRadius, overlapBuffer);
|
||||
Vector3 separationOffset = Vector3.zero;
|
||||
int overlapCount = 0;
|
||||
|
||||
@@ -346,7 +366,7 @@ namespace Colosseum.Enemy
|
||||
if (!TryGetPlayerCharacterController(overlapBuffer[i], out CharacterController playerController))
|
||||
continue;
|
||||
if (!Physics.ComputePenetration(
|
||||
bodyCollider, transform.position, transform.rotation,
|
||||
bodyCollider, bodyCollider.transform.position, bodyCollider.transform.rotation,
|
||||
playerController, playerController.transform.position, playerController.transform.rotation,
|
||||
out Vector3 separationDirection, out float separationDistance))
|
||||
{
|
||||
@@ -371,34 +391,273 @@ namespace Colosseum.Enemy
|
||||
|
||||
private bool IsTouchingPlayer()
|
||||
{
|
||||
if (bodyCollider == null)
|
||||
return false;
|
||||
|
||||
float scanRadius = GetPlayerDetectionRadius();
|
||||
int count = Physics.OverlapSphereNonAlloc(transform.position, scanRadius, overlapBuffer);
|
||||
int count = Physics.OverlapSphereNonAlloc(bodyCollider.bounds.center, scanRadius, overlapBuffer);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (!TryGetPlayerCharacterController(overlapBuffer[i], out CharacterController playerController))
|
||||
continue;
|
||||
|
||||
Vector3 toPlayer = playerController.transform.position - transform.position;
|
||||
toPlayer.y = 0f;
|
||||
if (toPlayer.magnitude < GetRequiredSeparationDistance(playerController))
|
||||
if (Physics.ComputePenetration(
|
||||
bodyCollider, bodyCollider.transform.position, bodyCollider.transform.rotation,
|
||||
playerController, playerController.transform.position, playerController.transform.rotation,
|
||||
out _, out float separationDistance)
|
||||
&& separationDistance > 0.0001f)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private Vector3 LimitHorizontalDeltaAgainstPlayerContact(Vector3 deltaPosition)
|
||||
{
|
||||
Vector3 horizontalDelta = new Vector3(deltaPosition.x, 0f, deltaPosition.z);
|
||||
if (horizontalDelta.sqrMagnitude <= 0.000001f)
|
||||
return deltaPosition;
|
||||
|
||||
horizontalDelta = ClampHorizontalDeltaToPlayerSurface(horizontalDelta);
|
||||
if (horizontalDelta.sqrMagnitude <= 0.000001f)
|
||||
{
|
||||
deltaPosition.x = 0f;
|
||||
deltaPosition.z = 0f;
|
||||
return deltaPosition;
|
||||
}
|
||||
|
||||
if (TryGetPlayerContactBlockDirection(out Vector3 currentBlockDirection) &&
|
||||
IsBlockedByContactForwardHemisphere(horizontalDelta, currentBlockDirection))
|
||||
{
|
||||
horizontalDelta = Vector3.zero;
|
||||
}
|
||||
|
||||
if (horizontalDelta.sqrMagnitude > 0.000001f &&
|
||||
TryGetProjectedPlayerContactBlockDirection(horizontalDelta, out Vector3 projectedBlockDirection) &&
|
||||
IsBlockedByContactForwardHemisphere(horizontalDelta, projectedBlockDirection))
|
||||
{
|
||||
horizontalDelta = ClampHorizontalDeltaBeforePlayerOverlap(horizontalDelta);
|
||||
}
|
||||
|
||||
deltaPosition.x = horizontalDelta.x;
|
||||
deltaPosition.z = horizontalDelta.z;
|
||||
return deltaPosition;
|
||||
}
|
||||
|
||||
private Vector3 ClampHorizontalDeltaToPlayerSurface(Vector3 horizontalDelta)
|
||||
{
|
||||
if (bodyCollider == null || horizontalDelta.sqrMagnitude <= 0.000001f)
|
||||
return horizontalDelta;
|
||||
|
||||
if (!WouldCrossPlayerSurface(horizontalDelta))
|
||||
return horizontalDelta;
|
||||
|
||||
if (WouldCrossPlayerSurface(Vector3.zero))
|
||||
return Vector3.zero;
|
||||
|
||||
float min = 0f;
|
||||
float max = 1f;
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
float mid = (min + max) * 0.5f;
|
||||
if (WouldCrossPlayerSurface(horizontalDelta * mid))
|
||||
max = mid;
|
||||
else
|
||||
min = mid;
|
||||
}
|
||||
|
||||
return horizontalDelta * min;
|
||||
}
|
||||
|
||||
private float GetPlayerDetectionRadius()
|
||||
{
|
||||
float enemyRadius = GetBodyHorizontalRadius();
|
||||
return enemyRadius + 1f + playerSeparationPadding;
|
||||
}
|
||||
|
||||
private float GetBodyHorizontalRadius()
|
||||
{
|
||||
if (bodyCollider != null)
|
||||
{
|
||||
Bounds bounds = bodyCollider.bounds;
|
||||
return Mathf.Max(bounds.extents.x, bounds.extents.z);
|
||||
}
|
||||
|
||||
return navMeshAgent != null ? navMeshAgent.radius : 0.5f;
|
||||
}
|
||||
|
||||
private void ApplyPlayerContactMovementLock()
|
||||
{
|
||||
if (!freezeHorizontalMotionOnPlayerContact ||
|
||||
navMeshAgent == null ||
|
||||
!navMeshAgent.enabled ||
|
||||
isAirborne)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!IsTouchingPlayer())
|
||||
return;
|
||||
|
||||
if (!navMeshAgent.isStopped)
|
||||
navMeshAgent.isStopped = true;
|
||||
|
||||
if (navMeshAgent.hasPath)
|
||||
navMeshAgent.ResetPath();
|
||||
}
|
||||
|
||||
private bool TryGetPlayerContactBlockDirection(out Vector3 blockDirection)
|
||||
{
|
||||
return TryGetPlayerContactBlockDirection(Vector3.zero, out blockDirection);
|
||||
}
|
||||
|
||||
private bool TryGetProjectedPlayerContactBlockDirection(Vector3 horizontalDelta, out Vector3 blockDirection)
|
||||
{
|
||||
return TryGetPlayerContactBlockDirection(horizontalDelta, out blockDirection);
|
||||
}
|
||||
|
||||
private bool TryGetPlayerContactBlockDirection(Vector3 horizontalOffset, out Vector3 blockDirection)
|
||||
{
|
||||
blockDirection = Vector3.zero;
|
||||
if (bodyCollider == null)
|
||||
return false;
|
||||
|
||||
float scanRadius = GetPlayerDetectionRadius() + horizontalOffset.magnitude;
|
||||
Vector3 scanCenter = bodyCollider.bounds.center + horizontalOffset;
|
||||
Vector3 bodyPosition = bodyCollider.transform.position + horizontalOffset;
|
||||
int count = Physics.OverlapSphereNonAlloc(scanCenter, scanRadius, overlapBuffer);
|
||||
int overlapCount = 0;
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (!TryGetPlayerCharacterController(overlapBuffer[i], out CharacterController playerController))
|
||||
continue;
|
||||
|
||||
if (!Physics.ComputePenetration(
|
||||
bodyCollider, bodyPosition, bodyCollider.transform.rotation,
|
||||
playerController, playerController.transform.position, playerController.transform.rotation,
|
||||
out Vector3 separationDirection, out float separationDistance) ||
|
||||
separationDistance <= 0.0001f)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Vector3 playerCenter = playerController.bounds.center;
|
||||
Vector3 towardPlayer = playerCenter - scanCenter;
|
||||
towardPlayer.y = 0f;
|
||||
if (towardPlayer.sqrMagnitude <= 0.0001f)
|
||||
{
|
||||
towardPlayer = -separationDirection;
|
||||
towardPlayer.y = 0f;
|
||||
if (towardPlayer.sqrMagnitude <= 0.0001f)
|
||||
continue;
|
||||
}
|
||||
|
||||
blockDirection += towardPlayer.normalized;
|
||||
overlapCount++;
|
||||
}
|
||||
|
||||
if (overlapCount <= 0 || blockDirection.sqrMagnitude <= 0.0001f)
|
||||
{
|
||||
blockDirection = Vector3.zero;
|
||||
return false;
|
||||
}
|
||||
|
||||
blockDirection.Normalize();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsBlockedByContactForwardHemisphere(Vector3 horizontalDelta, Vector3 blockDirection)
|
||||
{
|
||||
if (horizontalDelta.sqrMagnitude <= 0.000001f || blockDirection.sqrMagnitude <= 0.0001f)
|
||||
return false;
|
||||
|
||||
float blockedAmount = Vector3.Dot(horizontalDelta, blockDirection);
|
||||
return blockedAmount >= 0f;
|
||||
}
|
||||
|
||||
private Vector3 ClampHorizontalDeltaBeforePlayerOverlap(Vector3 horizontalDelta)
|
||||
{
|
||||
if (bodyCollider == null || horizontalDelta.sqrMagnitude <= 0.000001f)
|
||||
return horizontalDelta;
|
||||
|
||||
if (!HasProjectedPlayerOverlap(horizontalDelta))
|
||||
return horizontalDelta;
|
||||
|
||||
if (HasProjectedPlayerOverlap(Vector3.zero))
|
||||
return Vector3.zero;
|
||||
|
||||
float min = 0f;
|
||||
float max = 1f;
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
float mid = (min + max) * 0.5f;
|
||||
if (HasProjectedPlayerOverlap(horizontalDelta * mid))
|
||||
max = mid;
|
||||
else
|
||||
min = mid;
|
||||
}
|
||||
|
||||
return horizontalDelta * min;
|
||||
}
|
||||
|
||||
private bool WouldCrossPlayerSurface(Vector3 horizontalOffset)
|
||||
{
|
||||
if (bodyCollider == null)
|
||||
return false;
|
||||
|
||||
float enemyRadius = GetBodyHorizontalRadius();
|
||||
float minimumSurfaceDistance = enemyRadius + playerSeparationPadding;
|
||||
float scanRadius = GetPlayerDetectionRadius() + horizontalOffset.magnitude;
|
||||
Vector3 scanCenter = bodyCollider.bounds.center + horizontalOffset;
|
||||
int count = Physics.OverlapSphereNonAlloc(scanCenter, scanRadius, overlapBuffer);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (!TryGetPlayerCharacterController(overlapBuffer[i], out CharacterController playerController))
|
||||
continue;
|
||||
|
||||
Vector3 closestPoint = playerController.ClosestPoint(scanCenter);
|
||||
Vector3 horizontalToSurface = closestPoint - scanCenter;
|
||||
horizontalToSurface.y = 0f;
|
||||
if (horizontalToSurface.magnitude <= minimumSurfaceDistance + 0.001f)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private float GetPlayerDetectionRadius()
|
||||
private bool HasProjectedPlayerOverlap(Vector3 horizontalOffset)
|
||||
{
|
||||
float enemyRadius = navMeshAgent != null ? navMeshAgent.radius : 0.5f;
|
||||
return enemyRadius + 1f + playerSeparationPadding;
|
||||
}
|
||||
if (bodyCollider == null)
|
||||
return false;
|
||||
|
||||
private float GetRequiredSeparationDistance(CharacterController playerController)
|
||||
{
|
||||
float enemyRadius = navMeshAgent != null ? navMeshAgent.radius : 0.5f;
|
||||
float playerRadius = playerController != null ? playerController.radius : 0.5f;
|
||||
return enemyRadius + playerRadius + playerSeparationPadding;
|
||||
float scanRadius = GetPlayerDetectionRadius() + horizontalOffset.magnitude;
|
||||
Vector3 scanCenter = bodyCollider.bounds.center + horizontalOffset;
|
||||
Vector3 bodyPosition = bodyCollider.transform.position + horizontalOffset;
|
||||
int count = Physics.OverlapSphereNonAlloc(scanCenter, scanRadius, overlapBuffer);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (!TryGetPlayerCharacterController(overlapBuffer[i], out CharacterController playerController))
|
||||
continue;
|
||||
|
||||
if (Physics.ComputePenetration(
|
||||
bodyCollider, bodyPosition, bodyCollider.transform.rotation,
|
||||
playerController, playerController.transform.position, playerController.transform.rotation,
|
||||
out _, out float separationDistance)
|
||||
&& separationDistance > 0.0001f)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool TryGetPlayerCharacterController(Collider overlapCollider, out CharacterController playerController)
|
||||
@@ -780,7 +1039,7 @@ namespace Colosseum.Enemy
|
||||
|
||||
if (!float.IsInfinity(maxDistance))
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, target.transform.position);
|
||||
float distance = TargetSurfaceUtility.GetHorizontalSurfaceDistance(transform.position, target);
|
||||
if (distance > maxDistance)
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user