I’ve been struggling with this for a few days now.
Here’s my problem. I have a character who, when you attack another character (via right click,) will move toward them via A* and then attack them. The only problem I’m having is figuring out how to tell my object that it has reached that final destination. (A return from A* telling me the path is complete is something I’m looking for.)
If you need my code I’ll post it - but there’s a lot so I am just starting with this for now. Anyone know if a* returns a ‘finished’ value, or how I can tell that A* finished the movement?
And - Does anyone know how to tell my seeker to stop moving or cancel it’s a* path finding as well? Any help will be greatly appreciated.
Thanks.
Jordan Foley
aka entevily
Just to post an update, I found a way to check this for myself. (Kind of.)
This is what worked for my program.
Basically, I checked if the distance between my OBJECTS position is close enough to the TARGETS position. If the distance was relatively small (and therefore would stop moving really soon) I set my isMoving value to false.
However, I had OBJECTS that would get blocked from moving by other OBJECTS, or not exactly make it within range of the TARGETS positions. In this case, I checked to see if the OBJECTS current position is the same the OBJECTS previous position every 200 updates. Within 200 updates, if the OBJECTS position has not moved than I can only assume that the OBJECT is no longer moving. So I set my isMoving value to false.
The code is below so if anyone can think of anything simpler please let me know.
void CheckIfMoving () { if (targetWaypoint != null) { if (Vector3.Distance (transform.position, targetWaypoint) <= maxWaypointDistance) { isMoving = false; } else isMoving = true; } justACounter += 1; if (justACounter >= 200) { myLastPos = myCurrentPos; myCurrentPos = transform.position; if (myLastPos != null) { if (myCurrentPos == myLastPos) { isMoving = false; } else { isMoving = true; } } justACounter = 0; } }
Jordan Foley
aka Entevily
Hi!
You can override OnTargetReached on the AIPath class.
I did this to enable callbacks via delegates:
`using UnityEngine;
using System.Collections;
public class CustomizedAIPath : AIPath
{
public delegate void targetReachedDelegate();
public targetReachedDelegate callback;
public override void OnTargetReached()
{
if (callback != null)
callback();
}
}`
This will inform your script then the seeker reached its target and stopped moving.
However, I don’t think it will do anything if your object is stuck, it will keep running into the obstacle.