A* Pathfinding Project

Updating AI Destination Setter target


#1

My issue is when I remove the target from the AIDestinationSetter field and add the new target the AI character won’t move after the first target.

Working with the Example15_2D scene I have added some UI to spawn my players or boardPiece. The boardPiece is a prefab created from the AI character from the example scene. I haven’t altered the components of the now AI prefab except for removing the target GameObject, that I deleted from the scene, from the AIDestinationSetter component.

I made the BoardPiece.cs to handle the turn based functions passing the target to the AIDestinationSetter.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Pathfinding {
    public class BoardPiece : MonoBehaviour
    {
        public BoardPieceSO boardPieceSO;
        public GameObject uICanvas;
        public UIInput uIInput;
        public bool active;
        private Transform target;
        AIDestinationSetter aIDestination;
        void Start()
        {
            uICanvas = GameObject.Find("UIInput Canvas");
            uIInput = uICanvas.GetComponent<UIInput>();
            aIDestination = GetComponent<AIDestinationSetter>();
        }
        void Update()
        {
            if(uIInput.goTo && active == true){
                target = uIInput.goTo.transform;
                uIInput.goTo = null;
                active = false;
            }
            if(uIInput.activeGoTo == true && target){
                aIDestination.target = target;
                if (Vector2.Distance(transform.position, target.position) < 0.1f){
                    uIInput.activeGoTo = false;
                    target = null;
                    aIDestination.target = null;
                } 
            }
        }
        void OnMouseDown()
        {
            active = true;
            if(boardPieceSO.moveable == true)
            {
                uIInput.gameBoard.SetActive(true);
            }
        }
    }
}

I’m guessing I need to also call something to reset the Seeker or a setting on the AI Lerp component.

Do I need to change the repath rate “Infinity” to something?

Thanks- :slight_smile:


#2

Hi

You need to change the repath rate. If it is set to infinity then the agent will never update its path. Set it to for example 1.0 and then it will recalculate its path every 1.0 seconds.
You can also call ai.SearchPath() to force it to recalculate its path immediately (do not call this every frame however).