Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Change player position on scene change

Discussion in 'Connected Games' started by DespairBear, Jul 13, 2018.

  1. DespairBear

    DespairBear

    Joined:
    Nov 1, 2013
    Posts:
    174
    Hey all,

    Pretty simple question, so I have it setup so player's aren't destroyed when the scene changes and I'm trying to set each player's position to one of the spawn points in the new scene. I'm using the NetworkLobbyManager and was curious what method is the most effective way to get each player's transform position set? I keep running into issues with each method I've tried where the client's position gets changes for a few frames (only visible from the debug console, not to the human eye) but it quickly gets reset.

    I don't believe it's an issue with the character controller as the client is able to set its position just fine. Any tips or thoughts would be greatly appreciated

    Cheers
     
  2. DespairBear

    DespairBear

    Joined:
    Nov 1, 2013
    Posts:
    174
    a bit more info, so this is the code I'm using to set the player position:

    Code (CSharp):
    1. private void Start()
    2.     {
    3.         if (isLocalPlayer)
    4.         {
    5.             SceneManager.sceneLoaded += OnLevelFinishedLoading;
    6.         }
    7.         DontDestroyOnLoad(gameObject);
    8.     }
    9.  
    10.     public void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
    11.     {
    12.         if(scene.name == "Lobby")
    13.         {
    14.             return;
    15.         }
    16.         else
    17.         {
    18.             if(isLocalPlayer)
    19.             {
    20.                 Debug.Log("is local player!");
    21.                 transform.position = GameObject.FindGameObjectWithTag("Spawn").transform.position;
    22.             }
    23.         }
    24.     }
    The host's position is set just fine. However, the client, the position gets set and then is quickly reset back to the position it was in before the scene change.

    Would there be a better more centralized point to put this functionality? Perhaps overriding a method within the NetworkManager? New to multiplayer dev