Its a 3D platformer, using seeker, patrol, rich ai, and ai destination setter and they are attached to my patrolling enemy. The ai destination doesnt walk around walls when following the player.
here is my enemy controller script:
using UnityEngine;
using Pathfinding;
public class EnemyController : MonoBehaviour
{
private Animator animator;
private RichAI richAi;
private Patrol patrol;
private AIDestinationSetter aiDestinationSetter;
public bool IsAlive = true;
private bool IsAttacking = false;
[SerializeField] private float chaseRange;
[SerializeField] private float attackRange;
private float timeBetweenAttacks = 1f;
private float attackCounter;
private void Awake()
{
animator = GetComponent<Animator>();
richAi = GetComponent<RichAI>();
patrol = GetComponent<Patrol>();
aiDestinationSetter = GetComponent<AIDestinationSetter>();
}
private void Update()
{
// Check if player in range
if (aiDestinationSetter.target != null)
{
// Rotation and look at target
transform.LookAt(PlayerController.MyInstance.transform, Vector3.up);
transform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y, 0f);
float distanceToPlayer = Vector3.Distance(transform.position, aiDestinationSetter.target.transform.position);
if (distanceToPlayer <= chaseRange && IsTargetReachable(aiDestinationSetter.target.transform.position))
{
if (distanceToPlayer <= attackRange)
{
IsAttacking = true;
richAi.canMove = false;
attackCounter -= Time.deltaTime;
if (attackCounter <= 0)
{
animator.SetTrigger("Bite Attack");
HealthManager.MyInstance.TakeDamage(1);
attackCounter = timeBetweenAttacks;
}
}
else
{
IsAttacking = false;
richAi.canMove = true;
}
}
else
{
patrol.enabled = true;
IsAttacking = false;
aiDestinationSetter.target = null;
}
}
if (!IsAttacking)
{
animator.SetBool("moving", richAi.velocity.magnitude > 0.2f);
}
else
{
animator.SetBool("moving", false);
}
}
public void SetTarget(Transform target)
{
if (aiDestinationSetter.target == null)
{
aiDestinationSetter.target = target;
richAi.destination = target.transform.position;
patrol.enabled = false;
}
}
private bool IsTargetReachable(Vector3 targetPos)
{
var graph = AstarPath.active.data.recastGraph;
var targetNode = graph.PointOnNavmesh(targetPos, null);
return targetNode != null;
}
}