Search Unity

New input system : Get new Player object at spawn

Discussion in 'Multiplayer' started by Nadrielle, Apr 12, 2020.

  1. Nadrielle

    Nadrielle

    Joined:
    Mar 26, 2020
    Posts:
    1
    Hello,

    I'm using the new Input System to make a 2D Local Multiplayer game and I basically need to retrieve the player spawned when he click the button.

    What i'm doing is : Having an Input Manager using Unity Events, and retrieving this in a function.

    upload_2020-4-12_10-15-43.png

    Code (CSharp):
    1. public void PlayerJoined()
    2.     {
    3.         Debug.Log("Calling Gamemanager New Player joined");
    4.         FindObjectOfType<GameManager>().PlayerJoined();
    5.     }
    Is it possible to add a parameter in the "PlayerJoined(Callback)" to retrieve the spawned GameObject ? and if so, what type ?

    Subquestion : I'm going to change the position and color of the player for each spawn. I'll have a list for the color and one for the spawn location. Each spawned player will be added to a list with specific color/spawn.
    Is it a good way to go, or is there good classic way to do this ?

    Kind regards
     
  2. FeistyRodent

    FeistyRodent

    Joined:
    Aug 12, 2017
    Posts:
    2
    You can set Notification Behavior to Send Message
    PlayerInput.PNG
    When a player joins, an event will be sent to the Game Object that the Player Input Manager is on.

    Add the method OnPlayerJoined(PlayerInput playerInput) to a script that's on that same Game Object - You can then use the PlayerInput to get the player.

    Example:
    Code (CSharp):
    1.     private void OnPlayerJoined(PlayerInput playerInput)
    2.     {
    3.         SetPlayerPositionAndColor(playerInput.transform);
    4.     }
    5.  
    6.     void SetPlayerPositionAndColor(Transform player){}