0

Working on Multiplayer Game using photon pun 2 in Unity. I am trying to synchronize Box which is spawned when player trigger a function. So i need to synchronize game object for all players.enter image description here

Here is Spawning Codeenter image description here

private void SpawnBox()
{ 
    PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs" , "PickableCube"), transform.position, Quaternion.identity); 
} 

private void OnTriggerEnter(Collider other) 
{ 
    if (other.CompareTag("Player")) 
    { 
        SpawnBox(); 
    } 
}

I want to synchronize the game object for all players so if any player let the game it should not effect the game objects which are spawned in map . I tried PhotonNetwork instantiate , and also Photon view and photon view transform is applied on the box.

hijinxbassist
  • 3,667
  • 1
  • 18
  • 23
  • What was the issue when using PhotonNetwork.Instantiate? – hijinxbassist Jul 11 '23 at 17:00
  • it's start spawning extra boxes , and when i try to move it , it give move in wrong directions and keep rotating and moving in map on it's own – Mansoor tariq Jul 11 '23 at 17:07
  • Please include the instantiate code in your question, and any relevant code pertaining to the instantiation. – hijinxbassist Jul 11 '23 at 17:13
  • private void SpawnBox() { PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs" , "PickableCube"), transform.position, Quaternion.identity); } private void OnTriggerEnter(Collider other) { if (other.CompareTag("Player")) { SpawnBox(); } } – Mansoor tariq Jul 11 '23 at 17:21
  • One issue I noticed is that there is no constraint on who runs the Instantiate. Imagine there are 2 players total, and P1 enters the trigger. That player enters the collider on each client. So P1 sees them enter and executes, P2 also sees them enter and executes. Add a local player check before spawning the box. – hijinxbassist Jul 11 '23 at 19:46
  • Thank you now it's working good , One more thing when it's moving it goes to 0,0,0 position and after some time it comes back to drop position when player drop it . – Mansoor tariq Jul 11 '23 at 21:31
  • Please create a new question for each additional question you have. This helps people searching for answers in the future, otherwise it would get buried in an unrelated questions comments section. – hijinxbassist Jul 11 '23 at 21:39

1 Answers1

0

One issue I noticed is that there is no constraint on who runs the Instantiate function.

Imagine there are 2 players total and P1 enters the trigger.
That player enters the collider on each client.
P1 sees them enter and executes Instantiate.
P2 sees them enter and executes Instantiate.

Adding a local player check before spawning the box will prevent each client from instantiating the box.

private void SpawnBox()
{ 
    if (!photonView.isMine) return; // Not our player, don't spawn box

    PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs" , "PickableCube"), transform.position, Quaternion.identity); 
} 
hijinxbassist
  • 3,667
  • 1
  • 18
  • 23