Support Forum

AILerp - overriding OnTargetReached

Hey,
I am trying to override OnTargetReached from AILerp. I am not getting any errors, my function just does not run.

I am pretty new to game dev & working with Unity, so I feel like I am not getting the inheritance completely.

My AILerp prints the Debug.Log OnTargetReached but my function:
“public override void OnTargetReached()”
is never called, Debug.Log never prints.

I would appreciate your help. :slight_smile:

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

[RequireComponent(typeof(AILerp))]
[RequireComponent(typeof(AIDestinationSetter))]
public class CharacterMovementController : AILerp
{
	// movepoint controller will yield controll to this script when character is selected
	public GameObject gameManager;
	public GameObject movePoint;
	private MovePointController movePointController;
	private AIDestinationSetter destinationSetter;
	private CharacterSelection characterSelection;
	private TurnManager turnManager;
	private AILerp AILerp;
	private WorldTile _tile;


	public bool reachedDestinationThisTurn = false;


	protected override void Awake()
	{
		base.Awake();
		Debug.Log("overriden awake");

		seeker = GetComponent<Seeker>();

		turnManager = (TurnManager)gameManager.GetComponent<TurnManager>();

		movePointController = (MovePointController)movePoint.GetComponent<MovePointController>();

		characterSelection = (CharacterSelection)transform.GetComponent<CharacterSelection>();
		destinationSetter = (AIDestinationSetter)transform.GetComponent<AIDestinationSetter>();
		AILerp = (AILerp)transform.GetComponent<AILerp>();
	}

	public override void OnTargetReached()
	{
		Debug.Log("overriden ontarget reached");
		base.OnTargetReached();
		// do stuff
		OnReachedDestination();
	}
	public void OnReachedDestination()
	{
		Debug.Log("my destination reached.");

		movePointController.blockMovePoint = false;
		characterSelection.movedThisTurn = true;
		reachedDestinationThisTurn = true;

		turnManager.CharacterTurnFinished();
	}


	/*
	void Update(){
		if(AILerp.reachedEndOfPath && !characterSelection.movedThisTurn && !reachedDestinationThisTurn){
			OnReachedDestination();
		}
	}
	*/

	public void Move(Transform t)
	{
		//Vector3 currentPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
		if (destinationSetter != null)
		{

			var tiles = GameTiles.instance.tiles; // This is our Dictionary of tiles

			// check if selected tile is within range
			int posX = (int)Mathf.Floor(t.position.x);
			int posY = (int)Mathf.Floor(t.position.y);

			var worldPoint = new Vector3Int(posX, posY, 0);

			if (tiles.TryGetValue(worldPoint, out _tile))
			{
				if (_tile.WithinRange)
				{
					// move
					destinationSetter.target = t;
					AILerp.canSearch = true;
					AILerp.canMove = true;

					// disable movepoint
					movePointController.blockMovePoint = true;

					// clear highlight
					characterSelection.ClearHighlight();
				}
				else
				{
					Debug.Log("not within range");
				}
			}
		}
	}

}```

my AILerp:



public virtual void OnTargetReached () {

      canSearch = false;

      canMove = false;

      // stop animating player

      string tag = transform.tag;

      if(tag == "ControlledByPlayer"){

        isoRenderer = GetComponentInChildren<IsometricCharacterRenderer>();

        Vector2 inputVector = new Vector2(0f, 0f);

        isoRenderer.SetDirection(inputVector);

      }

      Debug.Log("astar on target reached");

    }

astar

It started working when I removed my code from AILerp OnTargetReached.

Also keep in mind that OnTargetReached is kinda deprecated. You should use the ai.reachedDestination property instead (or ai.reachedEndOfPath in some cases). See https://arongranberg.com/astar/docs/iastarai.html#reachedDestination

1 Like

Hey Aron,

thanks for the answer, it saddens me to find out that OnTargetReached is kinda deprecated. I am proud of my movement implementation that uses this function :slight_smile:

I was trying to make an alternative to my current script that uses ai.reachedDestination but I don’t understand when is ai.reachedDestination bool reseted . I am setting destination with AIDestinationSetter script and setting a new destination does not seem to reset the ai.reachedDestination bool to false. What am I missing?