# Moving a character based on a list of Vector 3's

I have a client server scenario. On the server there is an AI character who chooses a random point on the map and pathfinds to it, when at that point it chooses a new point and pathfinds again etc. The path to each point is an array which I send to the client, then a character on the client goes to each point in the array, making it look as though it is in sync with the server's AI character. (I realise with Unet all I really need is a network identity script and network transform component but this is a means to an end using this method).

So when the server creates the array and sends its own AI character off on the path, this array is sent to the client, the client then puts it into a List. Then using this List the Clients character moves to each node smoothly looking just like the server. My problem is that sometimes it moves and others it sticks and I get an error

"ArgumentOutOfRangeException: Argument is out of range. Parameter name: index"

My code is as follows:

Script that sends path and receives:

public class AccessingTargetPos : NetworkBehaviour {
public List<Vector3> receivedArrays = new List<Vector3>();

[ClientRpc]
void RpcPosFromServerToGoTo(Vector3 [] path)
{
UnityEngine.Debug.Log ("Going to ... " + path);
receivedArrays.AddRange (path);
}

public void PrintPath(Vector3 [] path)
{
if (isServer)
{
UnityEngine.Debug.Log("Send Array ...");
RpcPosFromServerToGoTo (path);
UnityEngine.Debug.Log("Sent!!");

}
}

}


And my code that moves the client character based on the List:

public class ClientAiMoveToPosArray : MonoBehaviour {

AccessingTargetPos accessingTargetPos;
public float speed = 1;
public Vector3 endPoint;
int index = 0;
// Use this for initialization
void Start ()
{
accessingTargetPos = GameObject.Find ("A*").GetComponent<AccessingTargetPos> ();
}
// Update is called once per frame
void Update ()
{
transform.position = Vector3.MoveTowards (transform.position, accessingTargetPos.receivedArrays [index], speed * Time.deltaTime);

if(transform.position == accessingTargetPos.receivedArrays [index])
{
index++;
}
}
}


I don't understand why I am getting this error and why at times my clients character sticks. I assume I am going wrong somewhere in my ClientAiMoveToPosArray script. Am I wrong in there somewhere?

UPDATE

The error occurs for 3 reasons:

1. At the beginning when the client has no point to move to i.e. I start the server then the array is generated, however the client hasn't connected yet. If the client connects when the AI is moving along a path it has to wait until a new path is generated before moving.
2. If the AI character on the server gets stuck in an unwalkable area.
3. If the client is moving exactly like the server AI character. The error is appears here because when the array is generated it has to be sent over to the client, resulting in a slight delay.
• Off topic of your specific question, but have you considered using deterministic logic on client and server for this AI? That would make it so the client knows where the AI is and so does the server, and no network communication needs to take place. – Alan Wolfe Jul 20 '15 at 14:27
• @AlanWolfe i've never heard of that before, and googling it I cant find much on it, in comparison to most id still be pretty new to Unity. But Ty for your comment, the above is for research, so your point is another to go on – CH99 Jul 21 '15 at 9:44
• Well, as the error says, you're trying to access an index which does not exist in your array, essentially the path "ended" and you're trying to get the next waypoint – tkausl Jul 21 '15 at 16:12
• @CH99, if you feel you've answered the question yourself then you should post it as an answer. The text beneath your update seems like it would do just fine! – Evan Oct 13 '15 at 13:53

## 1 Answer

Apologies for taking so long, but following the update included in the original question, the error occurs for 3 reasons:

1. At the beginning when the client has no point to move to i.e. I start the server then the array is generated, however the client hasn't connected yet. If the client connects when the AI is moving along a path it has to wait until a new path is generated before moving.
2. If the AI character on the server gets stuck in an unwalkable area.
3. If the client is moving exactly like the server AI character. The error is appears here because when the array is generated it has to be sent over to the client, resulting in a slight delay.