From b18cd06a01980470dfd01516ed1367716e22ba3a Mon Sep 17 00:00:00 2001 From: Vova <3emaster@gmail.com> Date: Thu, 13 Jul 2023 07:49:49 +0300 Subject: [PATCH] draw bounding box --- Assets/Scenes/Test.unity | 4 +- Assets/Scripts/Character.cs | 81 ++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 17 deletions(-) diff --git a/Assets/Scenes/Test.unity b/Assets/Scenes/Test.unity index b18cd83..a0ca944 100644 --- a/Assets/Scenes/Test.unity +++ b/Assets/Scenes/Test.unity @@ -1197,8 +1197,8 @@ CapsuleCollider2D: m_IsTrigger: 0 m_UsedByEffector: 0 m_UsedByComposite: 0 - m_Offset: {x: -0.17, y: 0.03} - m_Size: {x: 0.6, y: 0.89} + m_Offset: {x: 0, y: 0.03} + m_Size: {x: 0.7, y: 0.89} m_Direction: 0 --- !u!4 &331482265 stripped Transform: diff --git a/Assets/Scripts/Character.cs b/Assets/Scripts/Character.cs index 5ed3dc5..b14d49a 100644 --- a/Assets/Scripts/Character.cs +++ b/Assets/Scripts/Character.cs @@ -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; + } }