From 84d03a75c5fb90d8861f6f8e92e908525380cd27 Mon Sep 17 00:00:00 2001 From: Vova Date: Tue, 27 Jun 2023 15:08:55 +0300 Subject: [PATCH] Improve enemy logic --- Assets/Scripts/Character.cs | 14 ++++++++++++-- Assets/Scripts/EnemyAI.cs | 6 +++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/Character.cs b/Assets/Scripts/Character.cs index d428b90..843ad4c 100644 --- a/Assets/Scripts/Character.cs +++ b/Assets/Scripts/Character.cs @@ -38,8 +38,18 @@ public class Character : MonoBehaviour { var leftCheck = CheckBounds(Vector2.left, groundLayer); var rightCheck = CheckBounds(Vector2.right, groundLayer); - if(leftCheck) { isAllowLeft = false; } - if (rightCheck) { isAllowRight = false; } + if(leftCheck.collider!=null) { + isAllowLeft = false; + }else + { + isAllowLeft = true; + } + if (rightCheck.collider != null) { + isAllowRight = false; + }else + { + isAllowRight = true; + } isAllowVertical = false; _isFall = false; diff --git a/Assets/Scripts/EnemyAI.cs b/Assets/Scripts/EnemyAI.cs index 9ec0c31..5c5aeab 100644 --- a/Assets/Scripts/EnemyAI.cs +++ b/Assets/Scripts/EnemyAI.cs @@ -8,16 +8,16 @@ public class EnemyAI : Character float horizontal = 0; float vertical = 0; - if (Player.Instance.transform.position.x - transform.position.x > -0.1f && isAllowLeft) + if (Player.Instance.transform.position.x - transform.position.x < -0.05f && isAllowLeft) { horizontal = -1; } - else if (Player.Instance.transform.position.x - transform.position.x > 0.1f && isAllowRight) + else if (Player.Instance.transform.position.x - transform.position.x > 0.05f && isAllowRight) { horizontal = 1; } - else if (Mathf.Abs(Player.Instance.transform.position.y - transform.position.y) > 0.1f && isAllowVertical) + else if (Mathf.Abs(Player.Instance.transform.position.y - transform.position.y) > 0.05f && isAllowVertical) { var deltaY = Player.Instance.transform.position.y - transform.position.y; vertical = deltaY > 0 ? 1 : -1;