- Home /
Instantiating scene objects in Photon Multiplayer.
This code works correctly only for master client, but for other player objects spawn in the air in the wrong place, also, players see different instanciated objects in their games and do not see objects of each other. (instanciated objects have photon view with transform attached to it).
void Update ()
{
// if time to spawn a new game object
if (Time.time >= nextSpawnTime) {
// Spawn the game object through function below
GetComponent<PhotonView>().RPC("MakeThingToSpawn", PhotonTargets.All);
// determine the next time to spawn the object
nextSpawnTime = Time.time+secondsBetweenSpawning;
}
}
[PunRPC]
void MakeThingToSpawn ()
{
Vector3 spawnPosition;
Vector3 spawnPosition2;
// get a random position between the specified ranges
spawnPosition.x = Random.Range (xMinRange, xMaxRange);
spawnPosition.y = Random.Range (yMinRange, yMaxRange);
spawnPosition.z = Random.Range (zMinRange, zMaxRange);
spawnPosition2.x = Random.Range (xMinRange, xMaxRange);
spawnPosition2.y = Random.Range (yMinRange, yMaxRange);
spawnPosition2.z = Random.Range (zMinRange2, zMaxRange2);
GameObject spawnedObject = GameObject.Instantiate(spawnObjects, spawnPosition, transform.rotation) as GameObject;
GameObject spawnedObject2 = GameObject.Instantiate(spawnObjects, spawnPosition2, transform.rotation) as GameObject;
// make the parent the spawner so hierarchy doesn't get super messy
spawnedObject.transform.parent = gameObject.transform;
spawnedObject2.transform.parent = gameObject.transform;
}
You are telling each client to choose a random position. Also this will be running on every client.
The master should be the only one running the timer, selecting a random position, then sending that position to all clients which then instantiate the object.
Can you tell me how to send these parameters to other players please?
void Update()
{
if ( PhotonNetwork.is$$anonymous$$asterClient )
{
// if time to spawn a new game object
if ( Time.time > nextSpawnTime )
{
// get a random position between the specified ranges
Vector3 spawnPosition;
Vector3 spawnPosition2;
spawnPosition.x = Random.Range (x$$anonymous$$inRange, x$$anonymous$$axRange);
spawnPosition.y = Random.Range (y$$anonymous$$inRange, y$$anonymous$$axRange);
spawnPosition.z = Random.Range (z$$anonymous$$inRange, z$$anonymous$$axRange);
spawnPosition2.x = Random.Range (x$$anonymous$$inRange, x$$anonymous$$axRange);
spawnPosition2.y = Random.Range (y$$anonymous$$inRange, y$$anonymous$$axRange);
spawnPosition2.z = Random.Range (z$$anonymous$$inRange2, z$$anonymous$$axRange2);
// Spawn the game object through function below
GetComponent<PhotonView>().RPC( "$$anonymous$$akeThingToSpawn", PhotonTargets.All, spawnPosition, spawnPosition2 );
// deter$$anonymous$$e the next time to spawn the object
nextSpawnTime = Time.time + secondsBetweenSpawning;
}
}
}
[PunRPC]
void $$anonymous$$akeThingToSpawn( Vector3 spawnPosition, Vector3 spawnPosition2 )
{
GameObject spawnedObject = GameObject.Instantiate(spawnObjects, spawnPosition, transform.rotation) as GameObject;
GameObject spawnedObject2 = GameObject.Instantiate(spawnObjects, spawnPosition2, transform.rotation) as GameObject;
// make the parent the spawner so hierarchy doesn't get super messy
spawnedObject.transform.parent = gameObject.transform;
spawnedObject2.transform.parent = gameObject.transform;
}
Thanks a lot for your answer, now objects spawn in the same place, but for non-master player they are stuck in one place, and don`t have gravity applied to them.
I changed GameObject.Instantiate to PhotonNetwork.Instantiate, in Alucard`s code. Now 2 objects are being created near each other, and one of them i can controll with another player, and it is also visible for all players. I feel like i`m getting closer to solution. Thanks for your help
I also tried my old code with PhotonNetwork.Instantiate but it has the same problem - 2 objects are created, one of them is moving for one player, other - for second player. They are both visible. Do you know why is that? It seems to me, that gameObject should be instantiated only once, because we check if the player is master client and then call the function $$anonymous$$akeThingToSpawn().
Your answer
Follow this Question
Related Questions
How to request access for actions in PUN multiplayer? 0 Answers
How to make game objects move for all players in photon multiplayer? 0 Answers
Destroying entire parent object on one client but on the other destroying just the child. 1 Answer
How to make script work for all players Unity Photon? 1 Answer
Unity Collision Doesn't Work when Two Players Instantiated 1 Answer