Getting correct objects positions when joining a running game

Options
develax
develax ✭✭
If I'm not mistaken, when joining a running game it seems that the objects first appear at their initial positions on a new joined player's map and only a moment after they are moved to their current gameplay positions via PhotonView sync mechanism. It is usually hardly visible to the eye but sometimes it produces some artefacts such as the intersection of objects. How to make objects appear in the right places before the very first synchronization is performed?

Comments

  • develax
    Options
    Any ideas on how to manage this issue? Maybe there is some event that tells that all objects' been synchronized, and then a new player's character can be instantiated?
  • develax
    Options
    I noticed that only objects created by PhotonNetwork.Instantiate are buffered and then sent to a new client whenever it joins.
  • OneManArmy
    OneManArmy ✭✭✭
    Options
    
    public float smoothPosition = 5f;
    public float smoothRotation = 10f;
    public PhotonView pv;
    bool setPos = false;
    Vector3 correctPlayerPos = Vector3.zero;
    Quaternion correctPlayerRot = Quaternion.identity;
    
        public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.IsWriting)
            {
                stream.SendNext(transform.position);
                stream.SendNext(transform.rotation);
            }
            else
            {
                correctPlayerPos = (Vector3) stream.ReceiveNext();
                correctPlayerRot = (Quaternion) stream.ReceiveNext();
    
    	   if(!setPos) SetSpawnPosition(correctPlayerPos);
            }
        }
    
        void Update()
        {
            if(!pv.IsMine && setPos){
    	transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * smoothPosition);
    	transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * smoothRotation);
    	}	
        }
    	
    	void SetSpawnPosition(Vector3 correctPos)
    	{
    		if(Vector3.Distance(transform.position, correctPos) > 1.0f ){
    			transform.position = correctPos;
    		}
    		setPos = true;
    	}
    
  • develax
    Options
    @OneManArmy don't you make the object invisible until you call SetSpawnPosition? Without it, the instantiated object has time to flash on the screen for a moment.
  • OneManArmy
    OneManArmy ✭✭✭
    Options
    It's up to you. You can even move map above 0 on Y axis.
    I am disabling renderers.