Search Unity

create multiple players for the same connection

Discussion in 'Scripting' started by Eck0, Aug 16, 2018.

  1. Eck0

    Eck0

    Joined:
    Jun 6, 2017
    Posts:
    48
    How can I create more than one localplayer object?
    I do the following:


    [Server]
    void CreateNpc()
    {
    Vector3 posicion = transform.position;
    Quaternion rotacion = transform.rotation;
    GameObject esbirro = Instantiate(Resources.Load<GameObject>("Particulas/Esbirros/Esbirro1"), posicion, rotacion);
    NetworkServer.AddPlayerForConnection(GetComponent<NetworkIdentity>().connectionToClient, esbirro, 0 /*playerControllerId*/);
    NetworkServer.Spawn(esbirro); // If I do not do this, the object does not appear in the clients
    }

    This method create the object but localplayer = false

    Note: I do not want to do assing clientauthority since I handle the movement from the server and not from the client
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Unet is not designed to have multiple of the special Player GameObjects per client connection. I'd just spawn another prefab on the server and set references between that object and that client's Player GameObject to handle whatever you're trying to do.
     
  3. Eck0

    Eck0

    Joined:
    Jun 6, 2017
    Posts:
    48
    if I do spawnwithclientauthority I have to have activated the check localplayerauthority in networkidentity but this is a big problem for me, I do all the actions sending an order to the server and the server takes care of the action, for example from the client I make a call to the server and the server moves the player

    clicking right click from Cliente:

    [Client]
    void MoveToPoint(){
    cmd_MoveInServer(Input.mousePosition());
    }

    [Command]
    cmd_MoveInServer(Vector3 pos){
    Vector3 vPosicion = GetPositionValidate(pos);
    GetComponent<NavMeshAgent>().destination = vPosicion;
    }

    void Vector3 GetPositionValidate(){
    //Return position validate in map
    }



    This code works perfectly on my player (localplayer).
    I need to do the same but with an NPC that I can manage from the client, this npc should have islocalplayer = true but I can't do this when I spawn this npc from of server.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just put the commands to move the NPC inside a script on the Player object, or don't use a Command at all. Just send a Unet Message instead.
     
  5. Eck0

    Eck0

    Joined:
    Jun 6, 2017
    Posts:
    48
    I do not understand very well what you mean, there is no way to make calls [command] from a client who does not have authority?

    if it is not possible I will have to do NetworkClient.Send (opcode, msg), or is there another way?
    I do not want to use localplayerAuthority since this leads to errors, tricks, etc.

    I do not understand why a local player without client authority can make calls [Command] and a non-player object can only make calls if he has authority ...

    Why can not I create an npc with NetworkServer.AddPlayerForConnection (GetComponent <NetworkIdentity> (). ConnectionToClient, this.gameObject, 0) if it is assumed that PlayerController.MaxPlayersPerClient = 32 ...
     
    Last edited: Aug 17, 2018
  6. Eck0

    Eck0

    Joined:
    Jun 6, 2017
    Posts:
    48
    nobody knows about this?

    using localplayerauthority from a client I think it is a serious error since it is exposed to bugs such as invalid positions, exaggerated movement speeds, etc.

    Can you make cmd calls on an object with authority but without having mark localplayerauthority in the networkidentity? in this way I control animations and movements from the server and this already in charge of synchronizing the clients
     
  7. Eck0

    Eck0

    Joined:
    Jun 6, 2017
    Posts:
    48
    thanks for nothing, they listen to net and tremble, close the post