Ai destination setter not calculating path

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;
}

}

Hi

There are a few things here.

You seem to be using both the ai.destination property and the AIDestinationSetter. These are mutually exclusive. In fact all the AIDestinationSetter does is to set the ai.destination property every frame.
I recommend that you use the ai.destination property only and remove the AIDestinationSetter.

You are also using the Patrol script. This also sets the ai.destination property. So you probably want to mak sure to not set the destination yourself when that script is enabled.

its still not going around walls if I use rich ai instead to chase the player. How do I set ai.destination to null so it stop chasing the player?

I found out whats causing the problem that makes ai not walking around walls:

// Rotation and look at target
transform.LookAt(PlayerController.MyInstance.transform, Vector3.up);

I think its because RichAi has rotation enabled already. Is there away to get smooth rotation for rich ai? My ai is a snake.

The RichAI should have smooth rotation already. But if you are not happy with its rotation you can decouple it from the Transform using ai.updateRotation = false and then do the rotation yourself.

oh thanks! Also how to check if my ai.destination is != null? I am using if (!patrol.isActiveAndEnabled) right now.

The destination can never be null as it is a Vector3. The agent will however ignore it if is an unreasonable value like (float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity).

Oh thanks, the enable rotation on rich ai feel chunky. When i circle around the ai enemy agent, it turns 10 degrees at a time. Increasing the acceleration still the same issue except it just turns faster but still chunky.