Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Photon newbie, issue with map generation?

Discussion in 'Multiplayer' started by calvintmoss, Oct 1, 2014.

  1. calvintmoss

    calvintmoss

    Joined:
    Jul 4, 2014
    Posts:
    8
    So I am pretty new to photon but I got everything setup and multiple people can connect and move around smoothly without issue. My game is a space game and currently I have an empty that has a script on it called generatePlanets. In this script basically I just take my planet prefab and spawn it randomly throughout the world to create a unique experience. This works perfectly with one player, and the networking works perfectly without it, however when I have someone join the game, their machine generates its own random planets and they are NOT updated over the network, so one player sees a planet at some x,y and another does not. How should I fix this? Should I add a PhotonView on each of the planets and update on change and only run the generatePlanets() on room instantiate? Or is this this something that should be done some other (better) way.

    Here is the code to generate planets.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class generatePlanets: MonoBehaviour {
    5.  
    6.     public Transform planet;
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.         for(int i = 0; i < 10; i++)
    11.         {
    12.             Instantiate(planet, new Vector3(Random.Range(-1000,1000), Random.Range(-1000,1000), 0), Quaternion.identity);
    13.         }
    14.     }
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.     }
    19. }
    20.  
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    You could generate the positions of X planets and set them as room custom property. The benefit is: Any new player will have access to those when joining.
    Check out the PhotonNetwork.room.SetCustomProperties method. You can store the positions as Vector3[].

    Alternatively you could also just set a "Seed" to initialize Random on all clients which want to generate the planets. Random should generate the same sequence of positions when it is initialized with the same seed. This way, you only have to set a single value as custom property.
     
  3. calvintmoss

    calvintmoss

    Joined:
    Jul 4, 2014
    Posts:
    8
    This is what I ended up doing, I basically just count the number of planets on the map when you first hop in, if it is below the number it should be (first person) it generates them and seeds that information on the first player. Then I use that single value on all the others. Works pretty well actually