From eb9e4beaf7be74223e6023138a52a3d4d226dbd4 Mon Sep 17 00:00:00 2001 From: Valdimir Date: Thu, 13 Jul 2023 06:19:44 +0300 Subject: [PATCH] wip --- Assets/Scripts/Character.cs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/Assets/Scripts/Character.cs b/Assets/Scripts/Character.cs index 8c08a71..5ed3dc5 100644 --- a/Assets/Scripts/Character.cs +++ b/Assets/Scripts/Character.cs @@ -58,7 +58,7 @@ public abstract class Character : MonoBehaviour } _isOnBridge = downBlock == MapElementType.Bridge && !_isFalling; - + GetMapElement(Vector2.up); if (downBlock == MapElementType.Wall || _isOnLadder || _isOnBridge || downBlock == MapElementType.BreakableWall) { var leftCheck = GetMapElement(Vector2.left); @@ -130,16 +130,27 @@ public abstract class Character : MonoBehaviour } + private MapElementType GetMapElement(Vector3 direction) { - var bounds = _facingRight ? _capsuleCollider.bounds.min: _capsuleCollider.bounds.center; + var bounds = _capsuleCollider.bounds.min; - var cell = _tileMap.WorldToCell(bounds + direction); - Vector2 cell2d = new Vector2(cell.x + 0.5f, cell.y + 0.3f); - Vector2 cellSize = new Vector2(_cellSize.x - 0.3f, _cellSize.y - 0.05f); + //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); + + var playerBounds = _capsuleCollider.bounds.min; + playerBounds += new Vector3(0.01f, 0.01f); + + + DrawBounds(playerBounds, cellSize, Color.cyan); + + playerBounds = playerBounds + direction; + + var raycastHit = Physics2D.BoxCast(playerBounds, cellSize, 0f, Vector3.forward, .01f, _mapLayer); + + - cellSize = cellSize / 2; - var raycastHit = Physics2D.BoxCast(cell2d, cellSize, 0f, Vector3.zero, 0.01f, _mapLayer); MapElementType returnValue; Color color = Color.red; if (raycastHit) @@ -172,12 +183,17 @@ public abstract class Character : MonoBehaviour { returnValue = MapElementType.Empty; } + 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); - - return returnValue; } protected abstract void OnDeath();