draw bounding box
This commit is contained in:
+66
-15
@@ -137,19 +137,17 @@ public abstract class Character : MonoBehaviour
|
||||
|
||||
//var cell = _tileMap.WorldToCell(bounds + direction);
|
||||
//Vector2 cell2d = new Vector2(cell.x + 0.5f, cell.y + 0.3f);
|
||||
Vector2 cellSize = new Vector2(0.9f,0.8f);
|
||||
Vector2 cellSize = _cellSize;
|
||||
|
||||
var playerBounds = _capsuleCollider.bounds.min;
|
||||
playerBounds += new Vector3(0.01f, 0.01f);
|
||||
var playerBounds = _capsuleCollider.bounds.center;
|
||||
|
||||
|
||||
DrawBounds(playerBounds, cellSize, Color.cyan);
|
||||
//DrawBounds(playerBounds, cellSize, Color.cyan);
|
||||
|
||||
playerBounds = playerBounds + direction;
|
||||
|
||||
var raycastHit = Physics2D.BoxCast(playerBounds, cellSize, 0f, Vector3.forward, .01f, _mapLayer);
|
||||
var raycastHit = BoxCast(playerBounds, cellSize, 0f, Vector3.forward, .01f, _mapLayer);
|
||||
|
||||
|
||||
|
||||
|
||||
MapElementType returnValue;
|
||||
Color color = Color.red;
|
||||
@@ -183,18 +181,18 @@ public abstract class Character : MonoBehaviour
|
||||
{
|
||||
returnValue = MapElementType.Empty;
|
||||
}
|
||||
DrawBounds(playerBounds, cellSize, color);
|
||||
// DrawBounds(playerBounds, cellSize, color);
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
private static void DrawBounds(Vector2 cell2d, Vector2 cellSize, Color color)
|
||||
{
|
||||
Debug.DrawLine(cell2d, cell2d + new Vector2(cellSize.x, 0), color);
|
||||
Debug.DrawLine(cell2d + new Vector2(cellSize.x, 0), cell2d + new Vector2(cellSize.x, cellSize.y), color);
|
||||
Debug.DrawLine(cell2d, cell2d + new Vector2(0, cellSize.y), color);
|
||||
Debug.DrawLine(cell2d + new Vector2(0, cellSize.y), cell2d + new Vector2(cellSize.x, cellSize.y), color);
|
||||
}
|
||||
//private static void DrawBounds(Vector2 cell2d, Vector2 cellSize, Color color)
|
||||
//{
|
||||
// Debug.DrawLine(cell2d, cell2d + new Vector2(cellSize.x, 0), color);
|
||||
// Debug.DrawLine(cell2d + new Vector2(cellSize.x, 0), cell2d + new Vector2(cellSize.x, cellSize.y), color);
|
||||
// Debug.DrawLine(cell2d, cell2d + new Vector2(0, cellSize.y), color);
|
||||
// Debug.DrawLine(cell2d + new Vector2(0, cellSize.y), cell2d + new Vector2(cellSize.x, cellSize.y), color);
|
||||
//}
|
||||
|
||||
protected abstract void OnDeath();
|
||||
protected abstract void SetWalkingAnimation(bool isWalking);
|
||||
@@ -211,5 +209,58 @@ public abstract class Character : MonoBehaviour
|
||||
_facingRight = !_facingRight;
|
||||
}
|
||||
|
||||
static public RaycastHit2D BoxCast(Vector2 origen, Vector2 size, float angle, Vector2 direction, float distance, int mask)
|
||||
{
|
||||
RaycastHit2D hit = Physics2D.BoxCast(origen, size, angle, direction, distance, mask);
|
||||
|
||||
//Setting up the points to draw the cast
|
||||
Vector2 p1, p2, p3, p4, p5, p6, p7, p8;
|
||||
float w = size.x * 0.5f;
|
||||
float h = size.y * 0.5f;
|
||||
p1 = new Vector2(-w, h);
|
||||
p2 = new Vector2(w, h);
|
||||
p3 = new Vector2(w, -h);
|
||||
p4 = new Vector2(-w, -h);
|
||||
|
||||
Quaternion q = Quaternion.AngleAxis(angle, new Vector3(0, 0, 1));
|
||||
p1 = q * p1;
|
||||
p2 = q * p2;
|
||||
p3 = q * p3;
|
||||
p4 = q * p4;
|
||||
|
||||
p1 += origen;
|
||||
p2 += origen;
|
||||
p3 += origen;
|
||||
p4 += origen;
|
||||
|
||||
Vector2 realDistance = direction.normalized * distance;
|
||||
p5 = p1 + realDistance;
|
||||
p6 = p2 + realDistance;
|
||||
p7 = p3 + realDistance;
|
||||
p8 = p4 + realDistance;
|
||||
|
||||
|
||||
//Drawing the cast
|
||||
Color castColor = hit ? Color.red : Color.green;
|
||||
Debug.DrawLine(p1, p2, castColor);
|
||||
Debug.DrawLine(p2, p3, castColor);
|
||||
Debug.DrawLine(p3, p4, castColor);
|
||||
Debug.DrawLine(p4, p1, castColor);
|
||||
|
||||
Debug.DrawLine(p5, p6, castColor);
|
||||
Debug.DrawLine(p6, p7, castColor);
|
||||
Debug.DrawLine(p7, p8, castColor);
|
||||
Debug.DrawLine(p8, p5, castColor);
|
||||
|
||||
Debug.DrawLine(p1, p5, Color.grey);
|
||||
Debug.DrawLine(p2, p6, Color.grey);
|
||||
Debug.DrawLine(p3, p7, Color.grey);
|
||||
Debug.DrawLine(p4, p8, Color.grey);
|
||||
if (hit)
|
||||
{
|
||||
Debug.DrawLine(hit.point, hit.point + hit.normal.normalized * 0.2f, Color.yellow);
|
||||
}
|
||||
|
||||
return hit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user