New assets , enemy AI WIP

This commit is contained in:
2023-06-27 13:34:54 +03:00
parent 19edcb33ab
commit 719002cb7b
23 changed files with 470 additions and 135 deletions
+14 -8
View File
@@ -20,7 +20,9 @@ public class Character : MonoBehaviour
protected bool isAllowVertical = true;
protected bool isAllowHorisontal = true;
protected bool isAllowRight=true;
protected bool isAllowLeft= true;
private void Start()
{
_body = GetComponent<Rigidbody2D>();
@@ -29,13 +31,17 @@ public class Character : MonoBehaviour
protected void MoveTo(float inputHorizontal,float inputVertical)
{
var groundCheck = Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, Vector2.down, .1f, groundLayer);
var groundCheck = CheckBounds(Vector2.down, groundLayer);
float v_movement=0;
if (groundCheck || _isOnLadder)
{
var leftCheck = CheckBounds(Vector2.left, groundLayer);
var rightCheck = CheckBounds(Vector2.right, groundLayer);
if(leftCheck) { isAllowLeft = false; }
if (rightCheck) { isAllowRight = false; }
isAllowVertical = false;
isAllowHorisontal = true;
_isFall = false;
float h_movement = inputHorizontal;
if (h_movement > 0 && !_facingRight)
@@ -49,7 +55,7 @@ public class Character : MonoBehaviour
animator.SetBool("Walk", h_movement != 0);
if (IsLadder(Vector2.down))
if (CheckBounds(Vector2.down, ladderLayer))
{
isAllowVertical = true;
_isOnLadder = true;
@@ -57,7 +63,7 @@ public class Character : MonoBehaviour
if (v_movement > 0)
{
if (!IsLadder(Vector2.up))
if (!CheckBounds(Vector2.up,ladderLayer))
{
v_movement = 0;
}
@@ -75,9 +81,9 @@ public class Character : MonoBehaviour
}
}
private RaycastHit2D IsLadder(Vector2 direction)
private RaycastHit2D CheckBounds(Vector2 direction,LayerMask layer)
{
return Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, .1f, ladderLayer);
return Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, .1f, layer);
}
@@ -94,7 +100,7 @@ public class Character : MonoBehaviour
}
if (_isFall)
{
if (IsLadder(Vector2.down))
if (CheckBounds(Vector2.down,ladderLayer))
{
_isOnLadder = true;
}
+15 -9
View File
@@ -1,22 +1,28 @@
using UnityEngine;
using static UnityEditor.Searcher.SearcherWindow.Alignment;
public class EnemyAI :Character
public class EnemyAI : Character
{
private void Update()
{
float horizontal = 0;
float vertical = 0;
if (Mathf.Abs(Player.Instance.transform.position.y - transform.position.y) > 0.5f && isAllowVertical)
if (Player.Instance.transform.position.x - transform.position.x > -0.1f && isAllowLeft)
{
vertical = Player.Instance.transform.position.y - transform.position.y;
}else
if (Mathf.Abs(Player.Instance.transform.position.x - transform.position.x)>0.5f && isAllowHorisontal)
{
horizontal = Player.Instance.transform.position.x - transform.position.x;
horizontal = -1;
}
else if (Player.Instance.transform.position.x - transform.position.x > 0.1f && isAllowRight)
{
horizontal = 1;
}
else if (Mathf.Abs(Player.Instance.transform.position.y - transform.position.y) > 0.1f && isAllowVertical)
{
var deltaY = Player.Instance.transform.position.y - transform.position.y;
vertical = deltaY > 0 ? 1 : -1;
}
base.MoveTo(horizontal, vertical);
}
}