Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sending Updated GameObject to Linux Server

Discussion in 'Multiplayer' started by fieldforensics, Feb 11, 2018.

  1. fieldforensics

    fieldforensics

    Joined:
    Aug 23, 2017
    Posts:
    14
    I currently have my own Linux apache server running that I use to handle the server side of things for my game. I'm now attempting to allow for multiple players to play the same game at the same time. To do this, I want to send an updated location and 'animation' of their character to all the clients. I have an idea for a messy way of doing this. However, it'd involve me sending the updated coordinates within a string, then having the server identifying that string and sending that string to all the clients. Finally, I'd have to have each client extract the string into individual chars and converting them into integer/float values to update all the clients with an updated location of each of the client's whereabouts. The reason why I'd be sending it through a string is that currently, it's the only network function from the Unity C# API I'm aware about at the moment (myClient.Send(messageID, myMessage)). The client would have to update the player's animation as well (Walking animation) etc. of the character. This seems rather complicated, messy, and overall a potential performance issue. I'm aware of the Network Manage component script that Unity has already, however not sure how I'd use it to work with an already existing server instead of having it create its own little LAN server off of my localhost. I prefere to do these sorts of things myself, especially since I went through the trouble already getting the basic server-side functions setup. However, any solution would be satisfactory enough.
     
  2. Ellernate

    Ellernate

    Joined:
    Aug 25, 2017
    Posts:
    81
    In unet, sending messages between client/server should be done with NetworkMessage (https://docs.unity3d.com/Manual/UNetMessages.html) which is able to serialize most system and Unity types such as Vector3 for player positions.

    For the easiest implementation you could just derive from NetworkBehaviour and use syncvar, or attach the Network Transform / Network Animator components
     
  3. fieldforensics

    fieldforensics

    Joined:
    Aug 23, 2017
    Posts:
    14
    How would I go about sending a GameObject or even a Vector3 class to the server? I understand that I should create something like this in the server management class:
    Code (CSharp):
    1.     public class ScoreMessage : MessageBase
    2.     {
    3.         public int score;
    4.         public Vector3 scorePos;
    5.         public int lives;
    6.     }
    My version:
    Code (CSharp):
    1.     public class GameObjectMessage : MessageBase
    2.     {
    3.         public GameObject obj;
    4.     }
    However, the documentation doesn't make it clear as to how you'd send for example the ScoreMessage to the server from the client. However I basically tried (I was pretty sure it wouldn't work but was hoping for a miracle anyways)
    Code (CSharp):
    1.     public class GameObjectMessage : MessageBase
    2.     {
    3.         public GameObject obj;
    4.     }
    5.  
    6.   public static void sendGameObject(GameObject obj)
    7.     {
    8.         GameObjectMessage msg = new GameObjectMessage();
    9.         msg.obj = obj;
    10.         myClient.Send(256, msg);
    11.         Debug.Log("Sending... " + msg.obj.name);
    12.     }
    The error in the server console displays how the variable GameObject isn't initialized/null which is basically what I expected. Ideas?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can't send GameObjects like that. You create a class derived from MessageBase that is made up of basic standard and Unity types, like ints, floats, Vector3's, etc.
     
  5. fieldforensics

    fieldforensics

    Joined:
    Aug 23, 2017
    Posts:
    14
    Alright, how'd I go about sending Animations to the client? That's what I'm primarily concerned about. Positioning wouldn't be hard at all, however sending the appropriate animation corresponding to their position may be a bit tricky/messy. I could go ahead and have each client constantly check the current position vs the last position of the gameobject and set I suppose the specific animation frame/sprite, however I'd like it best if I could just send the appropriate animation along with the position of the gameobject for the client to simply update the gameobject it created as a duplicate. This could be benefitial in a number of ways, primarily I believe performance wise. I'd like the client to simply read and update, instead of reading, calculating, then updating.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Instead of trying to send an animation attached to a gameobject directly, I'd send an ID corresponding to an animation and then have the client instantiate the proper animation itself.