private void MoveToTarget(Action action = null)
{
ABPath path = CreatePath(_targetPosition);
path.callback += (Path p) =>
{
_npc.CurrentServerPosition = p.vectorPath.Last();
action?.Invoke();
_isMoving = false;
};
_isMoving = true;
AstarPath.StartPath(path);
}
I’m using a Unity headless server. Currently I’m generating a path on the server and then sending the end position of said path to the client to handle the actual movement, which is all working fine. With my current set up though, there’s no way for the server to know the actual current position of the agent.
The above code works fine, but the problem is of course that the callback is called when the path is initially created. Would it be possible to simulate movement on the generated path on the server without having any instantiated agents? Then, I could just assume that the server and client positions are more or less in sync, given that they should be running the same path with the same parameters.
EDIT:
Ended up doing it like this, but I’m still wondering if there’s a better way to do it:
private void MoveToTarget(Action action = null)
{
ABPath path = CreatePath(_targetPosition);
path.callback += (Path p) =>
{
_isMoving = true;
Debug.Log("Path generated. Points: " + p.vectorPath.Count);
foreach(Vector3 waypoint in p.vectorPath)
{
while (_npc.CurrentServerPosition != (Vector2)waypoint)
{
_npc.CurrentServerPosition = Vector2.MoveTowards(_npc.CurrentServerPosition, waypoint, Time.deltaTime * _npc.MovementSpeed);
}
Debug.Log("Waypoint reached.");
if (waypoint == p.vectorPath.Last())
{
Debug.Log("Path is complete.");
_isMoving = false;
action?.Invoke();
}
continue;
}
};
AstarPath.StartPath(path);
}