Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Spawn Player and give him control over his gameobject (SOLVED)

Discussion in 'Multiplayer' started by Deleted User, Nov 5, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hi,

    i cant find in the api, how to spawn a player on the server side, and give the player the control over it.
    I couldnt even find any possibility how to spawn a prefab on client side.

    I tried NetworkServer.SpawnWithClientAuthority(playerGameObject, netMsg.conn); but this dont work.
     
  2. Skerbey

    Skerbey

    Joined:
    Oct 15, 2015
    Posts:
    11
    First of all it seems that you are actually working with the HLAPI as the LLAPI is actually dealing with the Transport Layer of networking and the HLAPI encapsulates functionality built on top of the Transport Layer.

    Secondly here is example code from a project of mine:
    Code (CSharp):
    1. public GameObject SpawnPlayer(NetworkConnection conn) {
    2.         GameObject player = (GameObject)Instantiate(playerPrefab);
    3.         NetworkServer.AddPlayerForConnection(conn, player, 0);
    4. }
    Using AddPlayerForConnection for connection applies all the necessary settings that a player needs distinct from client owned objects. I believe this automatically spawns the player with client authority.

    More information can be found on the topic of player instantiation in the networking documentation: http://docs.unity3d.com/ScriptReference/Networking.NetworkServer.AddPlayerForConnection.html


    Regards,

    Skerbey
     
  3. Deleted User

    Deleted User

    Guest

    Thanks Skerbey :)