best way to sync alot of moving gamesobjects in a scene.

Options
So i am making a survival game with photon, but i need the host player to send Vector 3 positions of many moving gamepbjects, (zombies actually) .Since using photonview for each of them wouldn't be a good idea, i do this -

i have a manager common for all players in room , and the zombies that are moving are a part of a list.
The host player calls the function "sync_ai" sending the position along with their list index to all players.
This function gets called in the void Update().

example -
public List zombies;
void Update(){
for ( int i = 0 ; i < zombies.Count ; i ++ )
GetComponent().RPC("sync_ai",Photontargets.others,i, zombies[i].transform.position);
}

[PunRpc]
void sync_ai ( int a , Vector3 pos ){
zombies[a].transform.position = pos;
}


it seems to work fine, but the movement is not smooth and calling the function every frame seems stupid, is there a better way to do this?

Comments

  • OneManArmy
    Options
    You can use InvokeRepeating(), or while loop with yield WaitForSeconds(rate), to send rpc's with constant rate (few times a second) and then use Vector3.Lerp() to smooth out movement.

    P.S. You don't see problem here?
    for ( int i = 0 ; i < zombies.Count ; i ++ )
    GetComponent().RPC("sync_ai",Photontargets.others,i, zombies[i].transform.position);
    }
    why don't you send Vector3 array (just one rpc, to update position of all zombies)?
  • KingMaker
    Options
    thanks, haha my bad :p
    but is there a good way to sync positions of zombies if there over 300?

    what if i make the photonview on my gamemanager to observe this script , and set the observe option to Reliable Data Compressed--

    public List allthezombies; // close to 500 GOs

    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
    if (stream.isWriting) {
    for (int j = 0; j < allthezombies.Count ; j++)
    stream.SendNext (allthezombies[i].transform.position.x);
    }
    else {
    for (int j = 0; j < allthezombies.Count ; j++)
    allthezombies[i].transform.position = (Vector3) stream.ReceiveNext()
    }
    }