/// <summary> /// Cast a box or circle and find out if there is any object of the given layer mask in an area. /// </summary> /// <param name="raycastOrigins">Raycast origins (from a character driver).</param> /// <param name="isFacingRight">Cast to the left of to the right of a point (character).</param> /// <param name="offset">Cast offset from the origin.</param> /// <param name="targetLayer">Target layer mask.</param> /// <param name="range">Cast range (if a box).</param> /// <param name="radius">Cast radius (if a circle).</param> /// <returns>The first object hit by the cast.</returns> public static RaycastHit2D CheckObjectInArea(RaycastOrigins raycastOrigins, bool isFacingRight, Vector2 offset, LayerMask targetLayer, Vector2 range, float radius = 0) { Vector2 center = ((isFacingRight) ? raycastOrigins.rightCenter : raycastOrigins.leftCenter); // Add the offset. center.x += offset.x * ((isFacingRight) ? 1 : -1); center.y += offset.y; Vector2 dir = ((isFacingRight) ? Vector2.right : Vector2.left); RaycastHit2D hit; if (radius == 0) { // Align the cast to one of the sides. center.x += (range.x / 2) * ((isFacingRight) ? 1 : -1); hit = Physics2D.BoxCast(center, range, 0, dir, 0, targetLayer); } else { // Align the cast to one of the sides. center.x += (radius / 2) * ((isFacingRight) ? 1 : -1); hit = Physics2D.CircleCast(center, radius, dir, 0, targetLayer); } return(hit); }
bool IsTargetInRangeTest() { RaycastOrigins raycastOrigins = charDriver.GetRaycastOrigins(); bool isFacingRight = charDriver.IsFacingRight(); RaycastHit2D hit = CheckObjectInArea(raycastOrigins, isFacingRight, offset, targetLayer, range, radius); // May be necessary to check if the target is alive! return(hit.transform != null); }
public static void DisplayAttackAreaCircle(ICharacterDriver driver, float radius, Vector2 offset) { RaycastOrigins raycastOrigins = driver.GetRaycastOrigins(); bool isFacingRight = driver.IsFacingRight(); Vector2 center = ((isFacingRight) ? raycastOrigins.rightCenter : raycastOrigins.leftCenter); // Add offset. center.x += offset.x * ((isFacingRight) ? 1 : -1); center.y += offset.y; // Centralize. center.x += (radius / 2) * ((isFacingRight) ? 1 : -1); Gizmos.DrawWireSphere(center, radius); }
/// <summary> /// Display the attack area. /// </summary> /// <param name="driver">Character's driver.</param> /// <param name="range">Attack range.</param> public static void DisplayAttackArea(ICharacterDriver driver, Vector2 range, Vector2 offset) { RaycastOrigins raycastOrigins = driver.GetRaycastOrigins(); bool isFacingRight = driver.IsFacingRight(); Vector2 center = ((isFacingRight) ? raycastOrigins.rightCenter : raycastOrigins.leftCenter); // Add offset. center.x += offset.x * ((isFacingRight) ? 1 : -1); center.y += offset.y; // Centralize the central point. center.x += (range.x / 2) * ((isFacingRight) ? 1 : -1); Gizmos.DrawWireCube(center, new Vector3(range.x, range.y, 0)); }