Search code examples
unity-game-engineservernetwork-programmingclientmultiplayer

Unity Darkrift2 Game isn't connecting to server from different pc


I am making online multiplayer game with Darkrift2 and with Unity. I deeply know how to make games but i am new at online part. I made the game succesfully with LAN. I done the server side. But just the game that opened with server can connect. I am putting the server to my pc and go and open the game in another pc. It can't connect. I could definitely make an obvious mistake but IPV4 ADDRESS IS SAME AS PC'S IPV4 ADDRESS AND PORT NO IS SAME ON EVERY PC. Also there is no firewall ban, Game is opening in another pc's.

By the way probably you don't have to read the whole code, Just the connection part. Client NetworkManager.cs:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using DarkRift.Client.Unity;
    using DarkRift;
    using Tags;
    using DarkRift.Client;
    using UnityEngine.InputSystem;
    using System.Net;
    using System.Linq;
    
    public class NetworkManager : MonoBehaviour
    {
        IPAddress IPv4;
        public UnityClient client;
        public GameObject Bug;
        public Transform[] PlayerBegin;
        public Light DirLight;
    
        public List<GameObject> Bugs;
        
       
        private void Awake()
        {
    
            IPv4 = Dns.GetHostEntry(Dns.GetHostName())
                 .AddressList.First(
                     f => f.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
            Debug.Log(IPv4);
            client.Connect(IPv4, client.Port, true);
            
    
    
            client.MessageReceived += OnMessageReceived;
            //DontDestroyOnLoad(gameObject);
    
    
    
        }
        
        
        private void OnDestroy()
        {
    
            client.MessageReceived -= OnMessageReceived;
        }
        public void Interaction(byte Condition)//legdeathi de içine koyunca sorun yaratır mı? sanırım hayır
        {
            using (DarkRiftWriter writer = DarkRiftWriter.Create())
            {
                writer.Write(client.ID);
                writer.Write(Condition);
                
                using (Message message = Message.Create((ushort)Models.Tags0.InteractionType, writer))
                {
                    client.SendMessage(message, SendMode.Reliable);
                }
            }
    
        }
        
        public void PosRot(Vector3 Pos, Quaternion Rot, bool ThereIsPos)
        {
            XYZ Position=null;
    
            if (ThereIsPos)
            {
                Position = new XYZ();
                Position.X = Pos.x;
                Position.Y = Pos.y;
                Position.Z = Pos.z;
            }
            XYZ Rotation = new XYZ();
            Rotation.X = Rot.x;
            Rotation.Y = Rot.y;
            Rotation.Z = Rot.z;
            float RotW = Rot.w;
    
    
    
    
            using (DarkRiftWriter writer = DarkRiftWriter.Create())
            {
                writer.Write(client.ID);
    
                var tag = Models.Tags0.Rot;
                if (ThereIsPos)
                {
                    tag = Models.Tags0.PosRot;
                    writer.Write(Position);
                }
    
                writer.Write(Rotation);
                writer.Write(RotW);
                
                
                using (Message message = Message.Create((ushort)tag, writer))
                {
                   
                    client.SendMessage(message, SendMode.Unreliable);
                   
                }
    
    
    
            }
            
        }
        public int PlayerId;
        public ushort PlayerCount;
        private void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            using (Message message = e.GetMessage())
            {
                using (DarkRiftReader reader = message.GetReader())
                {
                    if (message.Tag == (ushort)Models.Tags0.InteractionType)
                    {
                        ushort Id = reader.ReadUInt16();
                        GameObject tarantula = Bugs[Id].transform.GetChild(0).gameObject;
                        byte Interaction = reader.ReadByte();
                        Spider s = tarantula.GetComponent<Spider>();
    
    
    
                        switch (Interaction)
                        {
                            case 0: s.Attack(); break;
                            case 1:
                                s.DefenceGetBug();
                                break;
                            case 2:
                                s.DefenceOff();
                                break;
                            case 3:
                                s.Death();
                                break;
                            case 4:
                                s.LegsHealthDown();
                                break;
                            default:
                                s.LegHealthDown(s.Legs[Interaction - 5], 3);
                                break;
                        }
    
    
                    }
    
                    byte ThereIsPos=0;
                    if(message.Tag == (ushort)Models.Tags0.PosRot)
                    {
                        ThereIsPos = 1;
    
    
                    }
                    else if(message.Tag == (ushort)Models.Tags0.Rot)
                    {
                        ThereIsPos = 2;
    
                    }
                    if (ThereIsPos != 0)
                    {
                        ushort Id = reader.ReadUInt16();
                        
                        GameObject tarantula = Bugs[Id].transform.GetChild(0).gameObject;
    
                        if (ThereIsPos == 1)
                        {
                            XYZ Position = reader.ReadSerializable<XYZ>();
                            tarantula.transform.position = new Vector3(Position.X, Position.Y, Position.Z);
    
                        }
                            
                        
                        XYZ Rotation = reader.ReadSerializable<XYZ>();
                        float RotW = reader.ReadSingle();
                        tarantula.transform.rotation = new Quaternion(Rotation.X, Rotation.Y, Rotation.Z, RotW);
    
    
    
    
    
                    }
    
    
                    if (message.Tag == (ushort)Models.Tags0.NewPlayer)
                    {
                        PlayerCount = (ushort)(reader.ReadUInt16() + 1);
                        //Debug.Log("PlayerCount : " + PlayerCount);
                        PlayerId = Bugs.Count;//Player No
                        //Debug.Log("PlayerId : " + PlayerId);
                        while (PlayerId < PlayerCount)
                        {
    
                            Debug.Log("Player Spawn" + PlayerId);
    
                            GameObject bug = Instantiate(Bug, PlayerBegin[PlayerId].position, Quaternion.identity, null);
                            
                            bug.name = "Bug" + PlayerId;
                            
                            Spider spider;
                            spider = bug.GetComponentInChildren<Spider>();
                            spider.nm = this;
                            spider.DirLight = DirLight;
                            //spider.transform.position = PlayerBegin[PlayerId].position;
                            //PosChange(spider.gameObject.transform.position);
                            if (client.ID != PlayerId)
                            {
                                //spider.enabled = false;
                                bug.transform.Find("Camera").gameObject.SetActive(false);
                                bug.transform.Find("CM FreeLook1").gameObject.SetActive(false);
                                bug.transform.Find("Camera Late").gameObject.SetActive(false);
                                bug.transform.Find("Canvas").gameObject.SetActive(false);
                                bug.GetComponentInChildren<PlayerInput>().enabled = false;
                            }
                            Bugs.Add(bug);
                            PlayerId++;
                        }
                    }
                }
            }
        }
    }

Server code .Net (only the main class):

using System;
using DarkRift.Server;
using DarkRift;
using Tags;

namespace Bug_Wars_Online
{
    public class BugWarsOnline : Plugin
    {
        public override bool ThreadSafe => false;

        public override Version Version => new Version(1, 0, 0);

        public BugWarsOnline(PluginLoadData pluginLoadData) : base(pluginLoadData)
        {
            ClientManager.ClientConnected += OnClientConnected;
            ClientManager.ClientDisconnected += OnClientDisconnected;
        }
        
       
        private void OnClientConnected(object sender, ClientConnectedEventArgs e)
        {
            Console.WriteLine("Connected");

            

            e.Client.MessageReceived += OnMessageReceived;

            using (DarkRiftWriter writer = DarkRiftWriter.Create())
            {
                writer.Write(e.Client.ID);
                writer.Write(0);
                using (Message message = Message.Create((ushort)Models.Tags0.NewPlayer, writer))
                {

                    foreach (IClient client in ClientManager.GetAllClients())
                    {
                        //if (client.ID != e.Client.ID)
                        {

                            client.SendMessage(message, SendMode.Reliable);
                            Console.WriteLine("Player" + client.ID + " Connected");
                            Console.WriteLine("ClientManager.Count" + ClientManager.Count);

                        }
                    }
                }
            }
        }
        
        private void OnClientDisconnected(object sender, ClientDisconnectedEventArgs e)
        {
            Console.WriteLine("Disconnected");
            //destroy et!




        }
        
        private void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            
            using (Message message = e.GetMessage())
            {
                if (message.Tag == (ushort)Models.Tags0.PosRot|| message.Tag == (ushort)Models.Tags0.Rot)
                {
                    foreach (IClient client in ClientManager.GetAllClients())
                    {
                        if (client.ID != e.Client.ID)
                        {
                            client.SendMessage(message, SendMode.Unreliable);


                        }
                    }

                }
                if (message.Tag == (ushort)Models.Tags0.InteractionType)
                {
                    foreach (IClient client in ClientManager.GetAllClients())
                    {
                        if (client.ID != e.Client.ID)
                        {
                            client.SendMessage(message, SendMode.Reliable);


                        }
                    }

                }





            }
        }
    }
}

Solution

  • Okay I should have enter the ipv4 address of server’s pc.