Pun 2 Coroutine with rpc inside, runs more times for some player objects

Options
I try to make a multiplayer snake and I have a coroutine that runs every second and translate snake head on the grid 1 centimeter to the left for example. Then after the movement is done I send an rpc to Others, with parameter the new position and tell them to move only my player to the desired position. This script runs for every player I PhotonNetwork.Instantiate but some of them seem to move faster on greed than others. The master client's head for example, apears to move a bit faster than the other heads, if I make them run in a row. I do something like this:

yield return new WaitForSeconds(speed);
if (photonView.IsMine) {
MoveLocal();
photonView.RPC("SendMove", RpcTarget.Others, transform.position);
}

(I am not colling extra functions that affects the move and I also checked how many times for every snake the function is called and it apears to be correct)
Why this happens?

Best Answer

Answers

  • Hi @BillyTheKid,

    The master client's head for example, apears to move a bit faster than the other heads, if I make them run in a row.


    Do you really mean faster in this case or do you mean earlier? If you mean earlier, I would say that this somehow is a 'normal' behaviour, because those Coroutines run at different times. If you mean faster, please let us know, how you do the movement. Please note, that there is also some delay between an object has moved on the local and on the other client's view.

    A hint: when you start the Coroutine, you can surround this call with the photonView.IsMine condition. In this case it only gets called on the Owner of the object and you won't have to check it each time the Coroutine gets processed.
    if (photonView.IsMine)
        StartCoroutine(...);
  • The movment is this: every time the MoveLocal() is executed the heads moves 1 meter to the left then sends this position to others with the rpc. If I put all the objects to run in a row and let them run for 1 minute to the left, one of them (usually the master client) will end up 20 meters ahead in both screens (the master's and the client's).
  • Do both clients run on the same computer? Is one client the Unity Editor and the other client a standalone build? What happens with two standalone builds?

    It would be also interesting to know, if the amount of calls of the Coroutine is the same on both clients. To check this, you could add an int to the class and increase its value after each yield return. To access this value easily, you can display it on screen by using Unity's OnGUI function. For example:
    void OnGUI
    {
        if (photonView.IsMine)
            GUILayout.Label("Number of calls: " + myValue);
    }
    The value will usually appear in the top left corner of the screen.
  • BillyTheKid
    edited December 2018
    Options
    I did what you said and I noticed the problem was that the cooroutines run faster on standalone windows build than in editor. But wouldn't that be a problem if I make a mobile version and a pc version of my game and they play on the same server? The coroutines may run with diferent speeds in each platform.