private Vector2 GetOverlapPositionForFrame(CollisionSide colSide, RaycastHit2D rayHit, Vector2 actualPos, float radius) { if (colSide == CollisionSide.Bottom || colSide == CollisionSide.Top) { actualPos.y = rayHit.collider.bounds.center.y; } else if (colSide == CollisionSide.Left || colSide == CollisionSide.Right) { actualPos.x = rayHit.collider.bounds.center.x; } BoxCollider2D boxCollider = rayHit.collider as BoxCollider2D; float fromCenterToBorder = boxCollider.size.x * boxCollider.transform.parent.localScale.x / 2; float distToMove = radius + fromCenterToBorder + distToMoveOnOverlap; Vector2 newPos = actualPos + colSide.GetOppositeDirectionVector() * distToMove; Debug.LogWarningFormat("dist to move on overlap: {0} newPos: {1}, from center to border: {2} ", distToMove, newPos, fromCenterToBorder); return(newPos); }
private Vector2 GetOverlapPositionForEnemyOrShip(CollisionSide colSide, RaycastHit2D rayHit, Vector2 actualPos, float radius) { BoxCollider2D boxCollider = rayHit.collider as BoxCollider2D; float fromCenterToBorder = 0; switch (colSide) { case CollisionSide.Bottom: case CollisionSide.Top: fromCenterToBorder = boxCollider.size.y * boxCollider.transform.localScale.y / 2; actualPos.y = rayHit.collider.bounds.center.y; break; case CollisionSide.Left: case CollisionSide.Right: actualPos.x = rayHit.collider.bounds.center.x; fromCenterToBorder = boxCollider.size.x * boxCollider.transform.localScale.x / 2; break; } float distToMove = radius + fromCenterToBorder + distToMoveOnOverlap; Vector2 newPos = actualPos + colSide.GetOppositeDirectionVector() * distToMove; return(newPos); }