Spawn Players.

Hello. I already asked this question in Unity questions section: http://answers.unity3d.com/questions/76 ... ayers.html

But i think it's better to ask it here.

I made Marco Polo tutorial, and decide to change it for my project.

All is the same, but instead of monsterprefab i use my own cubepfrefab (simple cube) and cube controller script instead of myThirdPersonControllerscript.

I'm creating player using this:
void OnJoinedRoom()
    {
        GameObject player = PhotonNetwork.Instantiate("Cube", new Vector3(0f, 0.5f, 0f), Quaternion.identity, 0);
        CubeController controller = player.GetComponent<CubeController>();
        controller.enabled = true;
    }

I added PhotonView component to my playerprefab and attach a NetworkManager script with SerializeView to Observe field:
public void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting)
        {
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
        }
        else
        {
            this.correctPlayerPos = (Vector3)stream.ReceiveNext();
            this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
        }
    }

When i create a room, everything is ok, the first cube is created and moving. But when i connect to the existing room, both cubes appears in the same start position, and in the next frame the first cube moving to its actual pos. Why is that happening?

Comments

  • no opinions? I made OnTriggerEnter event for each cube (player) and debug in it. When the new player spawns it launch OnTriggerEnter event for each existing player. How "to tell" a new player about other players positions before he spanws in the Scene?
  • It is happening (most likely) because you spawn all cubes at the same place.

    We dont "update" the Instantiate call but send it "as is" and then you get the current position like everyone else.
    So each player gets the "original" instantiate call as you do it initially. A moment later, the first updates will make the GameObject move and update its position to the current one.

    You could hide the GameObject until it gets the current position.
    In OnPhotonSerializeView, you could set a bool "gotInitialPos" and move the character there directly.