Enemy Movement and BasicPatrollingEnemyAI has added
This commit is contained in:
58
Assets/Scripts/AI/BasicPatrollingEnemyAI.cs
Normal file
58
Assets/Scripts/AI/BasicPatrollingEnemyAI.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Movement;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public class BasicPatrollingEnemyAI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] protected bool isMovingRight = false;
|
||||
|
||||
[Space]
|
||||
[Header("Left Detector")]
|
||||
[SerializeField] protected Vector2 leftCliffDetectorOrigin = Vector2.left;
|
||||
[SerializeField] protected Vector2 leftCliffDetectorSize = Vector2.one;
|
||||
|
||||
[Space]
|
||||
[Header("Right Detector")]
|
||||
[SerializeField] protected Vector2 rightCliffDetectorOrigin = Vector2.right;
|
||||
[SerializeField] protected Vector2 rightCliffDetectorSize = Vector2.one;
|
||||
|
||||
[Space]
|
||||
[Header("Right Detector")]
|
||||
[SerializeField] protected LayerMask groundLayerMask = ~(1 << 6); // Everything except the "Player" layer
|
||||
|
||||
protected IMovement movement = null;
|
||||
protected Collider2D[] nonAllocColliderArray = new Collider2D[10];
|
||||
|
||||
protected Vector2 leftCliffPosition => GetCliffPositionInWorld(leftCliffDetectorOrigin);
|
||||
protected Vector2 rightCliffPosition => GetCliffPositionInWorld(rightCliffDetectorOrigin);
|
||||
|
||||
protected bool IsLeftDetectorCollided => Physics2D.OverlapBoxNonAlloc(leftCliffPosition, leftCliffDetectorSize, 0, nonAllocColliderArray, groundLayerMask) != 0;
|
||||
protected bool IsRightDetectorCollided => Physics2D.OverlapBoxNonAlloc(rightCliffPosition, rightCliffDetectorSize, 0, nonAllocColliderArray, groundLayerMask) != 0;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
movement = gameObject.AddComponent<EnemyMovement>();
|
||||
}
|
||||
|
||||
protected virtual void FixedUpdate()
|
||||
{
|
||||
if ((isMovingRight && !IsRightDetectorCollided) || (!isMovingRight && !IsLeftDetectorCollided))
|
||||
isMovingRight = !isMovingRight;
|
||||
|
||||
movement.Move(isMovingRight ? 1f : -1f);
|
||||
}
|
||||
|
||||
protected Vector2 GetCliffPositionInWorld(Vector2 cliffPosition)
|
||||
=> cliffPosition + (Vector2)transform.position;
|
||||
|
||||
protected virtual void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = IsLeftDetectorCollided ? Color.green : Color.red;
|
||||
Gizmos.DrawWireCube(leftCliffPosition, leftCliffDetectorSize);
|
||||
|
||||
Gizmos.color = IsRightDetectorCollided ? Color.green : Color.red;
|
||||
Gizmos.DrawWireCube(rightCliffPosition, rightCliffDetectorSize);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/AI/BasicPatrollingEnemyAI.cs.meta
Normal file
11
Assets/Scripts/AI/BasicPatrollingEnemyAI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 321c5495f0d597749bf29c3a2966aa4a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user