mercredi 15 juin 2016

Unity : networking part II

I create a tiny connection screen in a nex scene. This scene has no network, only a canvas to show a background picture, and panel/buttons.
To be exhaustive, I ve created a gameobject named PLayerOverScene I talked about in previous article. I linked to this gameobject, the playerscript script. I will store in it, some information, like the ip asked by the player.


Important field for me, now is IP and connect.
I will not speak about login and password for the moment. I will use a rest repository, I will connect to, to validate user/password in a further article.

As always, I will create a c# script to handle click button on connect : ShimConnection.
I create a single simple function:

 public void checkLogin()

I added the script as component to the button.
I create a onclick event on buttonscript component.


For now, when you click on connect button, you will call checkLogin function.

Travel

Now what you want is to load the game scene, and to link to the networked scene from server.

At connect click, I store the Ip into PlayerOverScene:

GameObject playerOverScene = GameObject.Find("PlayerOverScene");
PlayerScript plScript = playerOverScene.GetComponent<PlayerScript>();
plScript.setIpToGo(IP.text);

First of all, you have to load the new scene (I have only two scenes, so for the moment, I will put hardcoded name of the scene).

     SceneManager.LoadScene("scenes/level1");

The game scene will be loaded... now we have to connect to server.
I Will modify this previous code :
public void Start()
    {
         GameObject playerOverScene = GameObject.Find("PlayerOverScene");
        if (playerOverScene == null)
        {
            bool result =this.StartServer();
        }else
        {
            this.StartClient();
        }   
}

To


public void Start()
    {
         GameObject playerOverScene = GameObject.Find("PlayerOverScene");
        if (playerOverScene == null)
        {
            bool result =this.StartServer();
        }else
        {
           PlayerScript ps = playerOverScene.GetComponent<PlayerScript>();
            if (ps)
            {
                this.networkAddress = ps.getIpToGo();

            }
            this.StartClient();
        }   
}

I grab back the Ip store into playerOverScene object to determine to modify internal variable (networkAdress).
I start client, I am connected (I hope) to the server.

Quite simple...


Next step, I will describe how to spawn the player ship...

jeudi 9 juin 2016

Unity : networking part 1

In the way, to have a dedicated server, and client able to connect to it, you can use standard script from unity. One of them, draw a little hud, where you can choose to launch standalone server, a client, or client hosting server.

Another script is more usefull and manage all events upon network : NetworkManager.

For my purpose, I create my onw c# script, inheriting of networkmanager. I wish to override several events, and function, so it is the best way to do. Its name will be ShimNetworkManager.

As in any script, you have a start and update function. In the start function, I will start a server, or start a client.

When the scene will start, this script must be runned. For this, I will add in my scene, a simple GameObject. I will name it NetworkManager.
I will add as component, my newly ShimNetworkManager script.

For now, as you will launch the game, it will execute automatically this script. But how to know if I am a server, or a client?

From my mind, it will be a server, if the game is loading directly this scene.
If the load of the scene comes from an another scene, it is a player trying to connect to a client. It is my assumption.

Pass GameObject scene to scene


I spent lot of time on this question.
When you load a scene, you destroy the old one, and all object belonging to this scene, even those you have instantiated by a script.
Finally, I discover a trick that allow to pass scene to scene some GameObject. So, to know if this is a client that is loading the scene, I test the existence of this object.

I create a second scene, named authentificationLevel. In this scene, I create a GameObject named PlayerOverScene.
I create a c# script named playerScript which will contain in further articles, the information linked to user (name, id, ....).

To allow to an object to be not destroyed, when you change your scene, you have to code this :

void Awake()
    {
        DontDestroyOnLoad(transform.gameObject);
    }

This line of code save your life.

For now, in all scene you will go, this object will remain, and you have a good way to store game information (score, player information, server information,....).

Back to Networking : Client or Server

In ShimNetworkManager, I can test this PLayerOverScene Object to know if I have to run server or client.
public void Start()
    {
         GameObject playerOverScene = GameObject.Find("PlayerOverScene");
        if (playerOverScene == null)
        {
            bool result =this.StartServer();
        }else
        {
            this.StartClient();
        }   
}








mercredi 8 juin 2016

Unity : Shimstar ; reloaded

OK let's try to create a multiplayer space game with 6 degrees of freedom.

I am not sure to follow the right way to create a game from scratch, I will write articles depending of what I consider to be important to know to create a multiplayer game, with physics.

SkyBox


Because, I like to be in the atmosfear, I ve created a skybox using SpaceScape to grab 6 textures.

I create into a folder, named Materials a new Material called skybox. In the inspector, choose the shader (skybox/6 sided)


Next try to add your 6 images to have in the preview your skybox.

Now you can go to the top menu "Window", and choose lighting, to make your new material effective to your scene :

Modify light configuration, to have an ambient light to light your object into the scene.


Networking in Unity

A game is at first defines by different scene. A player will play into one scene, and can travel scene to another scene.
In a configuration of one server, and multiple client connected, you will have a server running for each scene you will create.
The client will loadLevel and connect to the server it needs.
Server must have authority on physics, and will receive input from client, and broadcast results to all client who needs it.
You don't have a global server which manage a world with subscene. It is important to understand this.
Indeed, you will to manage to connect to a scene, and to disconnect from the scene, to connect to another scene running elsewhere (different IP, or different port).

In Unity, each object of the scene, can have multiple component:
  • mesh
  • material
  • scripts
You can associate as much of script you need, to manage behaviour of the object.
For example, you will have 3 scripts:
  • network identity : standard unity script which provide an unique network identity
  • network transform : standard unity script which manage the data transfer of position and rotation of the object
  • your behaviour script : custom script, with your specific fields, and function to manage custom behaviour
Unity will manage network behaviour on object possessing networking script.

Next Step


I will try to describe enough in details the different mechanism, during construction of the game.

I will show you how to manage a connection scene, and connect to a server.

After that, I will show you how to manage physics through networking.