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

PlayerInput across scene loads

Discussion in 'Input System' started by thomasgc, Jan 30, 2019.

  1. thomasgc

    thomasgc

    Joined:
    Jan 29, 2019
    Posts:
    2
    Is there a recommended way to keep a reference to a player across scenes?

    Let's say I have a local multiplayer game where players choose an avatar in a character selection scene. I don't want to Instantiate my players game objects right away when they select a character (like PlayerInputManager does), because it is not the gameplay scene.

    How should I instantiate the proper character for each player after loading my gameplay scene, while still using the PlayerInput component?
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Think PlayerInput may need to gain some more flexibility to support several different avenues for handling this but it should be doable even at this relatively early point.

    Overall, my recommendation would be to not try to defer PlayerInput instantiation to after the game has moved from the lobby to the gameplay scene but rather perform PlayerInput joining in the lobby as if it is the game. Avoids having to keep data on the joins around and replicating some of what PlayerInput/InputUser are already doing.

    However, you'd probably not want the full player to be present in the lobby and potentially also don't want inputs on the player to work yet (or at least not gameplay inputs; UI inputs you probably already want working).

    To solve that, you could multiple directions. One way would be to initially spawn a very reduced player prefab and then later add additional components or parent new GOs to it. Another way would be to disable all components except PlayerInput and then later enabling the other ones (e.g. the rendering parts of the player) when starting gameplay.

    To have the players and manager survive the scene load, either move them to a separate scene or perform an additive load.

    To determine what inputs the player can use while in the lobby, differentiate by action map or passivate input on the player altogether. You may want UI input only in the lobby. Once PlayerInput supports UI, that should be simple.
     
    GilbertoBitt likes this.
  3. Shaolin-Dave

    Shaolin-Dave

    Joined:
    Apr 3, 2015
    Posts:
    32
    Here's my solution to this (and other) issues:

    Rather than having the Player Input Manager object spawn prefabs for the player's character, I have it spawn empty prefabs called Player Manager (which still do have Player Input components). Unity's Player Input Manager will be a singleton that persist scene-to-scene using DontDestroyOnLoad(). My Player Manager object also persist scene-to-scene, but there will be one per actual human player.

    Any scene that will have GameObjects that I want the players to control will have a Player Spawner object (one per possible human player). It'll hold a prefab for whatever actual controllable GameObject the player should control. Most levels it'll be a regular playable character. On one scene it could be a cursor for a character select GUI.

    Whenever a scene loads, the Player Manager objects will look for any Player Spawner objects. The Player Spawner objects will spawn the Player GameObjects, and assign them to be controlled by the same device as it's relative Player Manager.

    In addition to tracking devices, the Player Manager can track other variables that need to be carreid from scene to scene.

    So far the only issue I can't solve is that the Player Index on the GameObjects with the same devices are still different, even though I set them to match. I guess this might become and issue when players leave the game, and rejoin, or others join.
     
    Last edited: Dec 10, 2021
    Awufumana and tardegast like this.