Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Life Alchemist · Dec 28, 2014 at 11:28 PM · networkingnetworkbasicbasics

Basic Networking

Short version: I can't establish a connection between clients.

Longer version: Below, I'm trying to implement a very simple direct connect multiplayer functionality without need of a master server. My direction is similar to the games Minecraft and The Forest. I am currently using a router (D-Link 615). I have port forwarded. I keep getting NAT target 0 not connected to NAT facilitator 67.225.180.24:50005 when I do RefreshIP. Also NAT target 24 errors when I use Join Game and type in my IP. I'm not sure if I need to do some firewall tinkering (and what part I need to do), or I'm just missing a simple step.

I have read up on networking with the main unity website and several tutorials (however they do not explain how to create a direct connect system or handle the error's I'm getting), so please do not give me links that explain how basic networking works. The only thing I could use clarifying on is NAT punchthrough as the Unity documentation isn't as clear for me to understand.

I've read several sources where it says master servers are not required. here and here.

I'm familiar with Photon Unity Networking, however, that limits how often the players can interact with each other unless I pay for more. My level of multiplayer should be doable by Unity without needing Photon.

 using UnityEngine;
 using System.Collections;
 
 public class startScreen : MonoBehaviour {
 
     float screenWidth;
     float screenHeight;
     string ipAddress;
     string port;
 
     string myIP;
     int myPort;
 
     string networkStatus;
 
     bool gameStarted = false;
 
     public Object fpc; //first person controller
 
     // Use this for initialization
     void Start () {
         screenWidth = Screen.width-(Screen.width*.1f);
         screenHeight = Screen.height-(Screen.height*.1f);
         ipAddress = "";
         port = "";
         networkStatus = "";
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void OnGUI () {
         if(!gameStarted){
             GUI.BeginGroup(new Rect(Screen.width/2-(Screen.width*.9f/2), Screen.height/2-(Screen.height*.9f/2),
                                     Screen.width-(Screen.width*.1f), Screen.height-(Screen.height*.1f)));
             GUI.Box(new Rect(0,0,Screen.width-(Screen.width*.1f),Screen.height-(Screen.height*.1f)),"Welcome to the game!");
 
             if(GUI.Button(new Rect(10,30,100,20), "Refresh IP")){
                 getMyIP();
             }
 
             //Host IP and Port
             GUI.Label(new Rect(120,30,20,20),"IP: ");
             GUI.Label(new Rect(140,30,120,20),myIP);
             GUI.Label(new Rect(270,30,40,20),"Port:");
             GUI.Label(new Rect(310,30,80,20),myPort.ToString());
 
             int shift1 = 20;
             if(GUI.Button(new Rect(10,30+shift1,100,20), "Host Game")){
                 HostGame();
             }
 
             if(GUI.Button(new Rect(10,54+shift1,100,20), "Join Game")){
                 JoinGame();
             }
 
             //input fields to join a game
             GUI.Label(new Rect(120,54+shift1,20,20),"IP:");
             ipAddress = GUI.TextField(new Rect(140,54+shift1,120,20), ipAddress);
             GUI.Label(new Rect(270,54+shift1,40,20),"Port:");
             port = GUI.TextField(new Rect(310,54+shift1,80,20), port);
 
             if(GUI.Button(new Rect(10,78+shift1,100,20), "Start Game")){
                 StartGame();
             }
 
             GUI.Label(new Rect(10,100+shift1,Screen.width-(Screen.width*.1f)-20,100),networkStatus);
             GUI.EndGroup();
         }
     }
 
     void StartGame(){ //activates all connected members to rpc call to start game
         gameStarted = true;
         Camera.main.GetComponent<Transform>().gameObject.SetActive(false);
         Network.Instantiate(fpc,new Vector3(5,2,5),Quaternion.identity,0);
     }
 
     void HostGame(){
         Network.InitializeServer(5,80,!Network.HavePublicAddress());
     }
 
     void JoinGame(){
         Network.Connect(ipAddress, port);
     }
 
     void getMyIP(){
         Network.Connect("http://www.google.com");
         myIP = Network.player.externalIP;
         myPort = Network.player.externalPort;
 
         Debug.Log(Network.player.externalIP);
         Debug.Log(Network.player.externalPort);
         Debug.Log(!Network.HavePublicAddress());
     }
 
     void OnConnectedToServer() {
         networkStatus = "Connected to Server";
     }
 
     void OnDisconnectedFromServer() {
         networkStatus = "Disconnected from Server";
     }
 
     void OnFailedToConnect() {
         networkStatus = "Failed to Connect";
     }
 
     void OnPlayerConnected(){
         networkStatus = "Player has connected";
     }
 
     void OnPlayerDisconnected(){
         networkStatus = "Player has disconnected";
     }
 
     void OnServerInitialized(){
         networkStatus = "Server Initialized";
     }
 }
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by 03gramat · Jan 15, 2015 at 02:11 PM

Heres a tutorial that might help:

https://www.youtube.com/watch?v=QfzrpxO89tU

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Since switching to using offline & online scenes in the NetworkManager, Clients will not sync (UNET) 0 Answers

Client disconnect due to error 0 Answers

Moving another player character on Network 1 Answer

Multiplayer Moving Bullet 1 Answer

RPC vs WWW : Performance 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges