Search Unity

Self Hosted Unity Server

Discussion in 'Multiplayer' started by luckie12, Oct 17, 2016.

  1. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Hi!

    I am trying to make my unet running over a self-hosted server, where i can change the max connections to 50 players. But i dont know where to start to make it on my own server. It works over the Unity Servers! I already have a host where i can run the server on

    Can anyone help me with this?

    Thankyou!
     
  2. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    You don't need anything special. Add the code to start a server in your game by using NetworkServer.Listen() or NetworkManager and then just run the game on your server machine. clients should be able to connect using the machine's public IP and port you specified.
     
  3. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Hmm, i use the network manager script, but it keeps trying to connect to localhost ....
     
  4. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165

    I got it, but it laggs so hard!

    it is really jittery, it jumps from left to right... instead of smooth

    i also need a bit help with instantiating over network:
    I have a missile, that i want to be fired from a client, but everytime i press the keys (C and LEFT CLICK) it comes up with:

    Code (csharp):
    1.  
    2. Network configuration mismatch detected. The number of networked scripts on the client does not match the number of networked scripts on the server. This could be caused by lazy loading of scripts on the client. This warning can be disabled by the checkbox in NetworkManager Script CRC Check.
    3. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    4.  
    5.  
    6. Did not find target for sync message for 10
    7. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    8.  
    The 10 keeps counting up.
    both of my objects identity are set to LocalPlayer

    Code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Networking;
    5. using System;
    6.  
    7. public class MissileLauncher : NetworkBehaviour {
    8.  
    9.     [SyncVar]
    10.     public GameObject missile;
    11.     public GameObject target;
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (Input.GetButtonDown("Fire1") && Input.GetKey(KeyCode.C))
    20.         {
    21.             if (target == null)
    22.             {
    23.                 Debug.Log("Cannot find target");
    24.             }
    25.             else
    26.             {
    27.                 //Network.Instantiate(missile, transform.position, transform.rotation,0);
    28.                 CmdSpawnItem(transform.position, transform.rotation);
    29.             }
    30.         }
    31.     }
    32.  
    33.     [Command]
    34.     void CmdSpawnItem(Vector3 pos, Quaternion rot)
    35.     {
    36.         GameObject go = (GameObject)Instantiate(missile,pos, rot);
    37.         NetworkServer.Spawn(go);
    38.     }
    39.  
    40. }
    41.  
    42.  

    Screens:




    ^This one has to be instantiated

    And he is registred in the network manager,

    on the local AND the dedicated one
     
    Last edited: Oct 17, 2016
  5. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Why are you giving the missle Local Player Authority? That would be the first question I would address. It doesn't point to the error you're getting though.
     
  6. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    I gave it Local Player Authority because it keeps giving me an warning with something about trying to instantiate over the network without authority, when i Use Network.SpawnWithClientAuthority(go, connectionToClient); it doesnt work either...
     
  7. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    I think that i found the problem with the object instantiating:

    My server literally only has Network Manager and Network Manager HUD, but nothing to handle the incoming stuff,

    how would i do that ?

    My server is basicly a new scene with network manager and a hud, thats all, it accepts connections (ofcourse) but it doesnt handle anything,

    Can anyone help me with that ?

    How would i be able to send a CMD from the client to the server and instantiate, in this case, a missile...
     
    Last edited: Oct 18, 2016
  8. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    You should probably look into the Unity tutorials that show how to make use of Commands and Remote Procedure Calls.
     
  9. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Yeah!

    I tried:

    Running SpawnItem() from the client to the server, and at the serverside instantiate the object, but i forgot to add at the server [RPC] haha !
     
  10. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    So what i did:

    This is the client part:

    Code (csharp):
    1.  
    2.     [Command]
    3.     void CmdSpawnItem(Vector3 pos, Quaternion rot)
    4.     {
    5.         GetComponent<CheckSets>().RpcSpawnItem(missile, pos, rot);
    6.     }
    7.  
    and this is the server (CheckSets.cs):
    Code (csharp):
    1.  
    2.     [ClientRpc]
    3.     public void RpcSpawnItem(GameObject missile, Vector3 pos, Quaternion rot)
    4.     {
    5.         GameObject go = (GameObject)Instantiate(missile, pos, rot);
    6.         NetworkServer.Spawn(go);
    7.     }
    8.  
    its not giving any errors/warnings but it also does not instantiate the object
     
  11. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    87
    You've got this a little bit switched around.

    Your client would call the [Command] method CmdSpawnItem(), which is then, through the magic of UNET, executed on the servers instance of the script/object. So that's the method where you would want to call NetworkServer.Spawn()

    NetworkServer.Spawn takes care of telling all the clients to instantiate the thing. From then on it's up to you how that new object is handled by the network, if at all.
     
  12. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Oh i see! :O

    And that would also work with a server in a different scene?