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 Joshua4missions · Aug 08, 2014 at 05:28 PM · instantiatenetworknetwork.instantiate

Network.Instantiate doesn't create player 2 gameobject.

Hey everyone,

I just started learning about networking yesterday. I have a basic scene setup where I can create a room and have other players join it. The problem is that only the host's character shows up. When Player 2 joins he can see player 1's character moving around, but his does not exist.

To help distinguish between different players, the players' personal character is blue. All other players' characters are yellow.

Here is my Network Manager code: - (On an empty game object)

 using UnityEngine;
 using System.Collections;
 
 public class NetworkManager : MonoBehaviour {
 
     //Declare variables
     private string typeGame = "NetworkGame";
     private string gameName = "RoomName";
     public GameObject playerPrefab;  
     public GameObject spawnPoint;
     public int playerCount = 0;
 
 
     // Use this for initialization
     void Start () {
         //MasterServer.ipAddress = "127.00.1";
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     private void StartServer()
     {
         Network.InitializeServer (2, 25565, !Network.HavePublicAddress());
         MasterServer.RegisterHost (typeGame, gameName);
     }
 
     void OnServerInitialized()
     {
         Debug.Log ("[NETWORK]Server Initialized");
         SpawnPlayer ();
 
     }
 
     //General User Interface
     void OnGUI()
     {
         if (!Network.isClient && !Network.isServer) 
         {
             if (GUI.Button (new Rect(100,100,250, 100), "Start Server"))
                 StartServer();
             if (GUI.Button(new Rect(100, 250, 250, 100), "Refresh Hosts"))
                 RefreshHostList();
             if(hostList != null)
             {
                 for(int i = 0; i < hostList.Length; i++)
                 {
                 if(GUI.Button (new Rect(400, 100+(110*i), 300, 100), hostList[i].gameName))
                     JoinServer(hostList[i]);
                 }
             }
         }
     }
 
     //Host Information
 
     private HostData[] hostList;
 
     private void RefreshHostList()
     {
         MasterServer.RequestHostList(typeGame);
     }
     void OnMasterServerEvent(MasterServerEvent msEvent)
     {
         if(msEvent == MasterServerEvent.HostListReceived)
         {
             hostList = MasterServer.PollHostList();
         }
     }
 
     private void JoinServer(HostData hostData)
     {
         Network.Connect (hostData);
     }
     void OnConnecterToServer()
     {
         Debug.Log ("[NETWORK]Server Joined");
         SpawnPlayer ();
     }
 
     private void SpawnPlayer()
     {
         //Network.Instantiate(playerPrefab, new Vector3(990f, 5f, 990f), Quaternion.identity, 0);
         System.Threading.Thread.Sleep (1);
         Network.Instantiate (playerPrefab, new Vector3 (spawnPoint.transform.position.x, spawnPoint.transform.position.y, spawnPoint.transform.position.z), Quaternion.identity, 0);
         Debug.Log ("[NETWORK]playerName has joined the game.");
         playerCount += 1;
     }
 
 }
 
 

Here is the player script: - (on players character)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour {
 
     //Define Variables
     public int speed = 25;
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     //If player is mine, turn blue; if someone else, display as yellow
     void Update () {
         if (networkView.isMine) {
             InputMovement ();
             gameObject.renderer.material.color = new Color (0, 0, 255);
                 } else 
                     {
                     SyncedMovement();
                     gameObject.renderer.material.color = new Color(255,255,0);
                     }
     }
 
 
     //Player Movement
 
     void InputMovement()
     {
         if (Input.GetKey (KeyCode.W))
             rigidbody.MovePosition (rigidbody.position - Vector3.forward * speed * Time.deltaTime);
         if (Input.GetKey (KeyCode.S))
             rigidbody.MovePosition (rigidbody.position + Vector3.forward * speed * Time.deltaTime);
         if (Input.GetKey (KeyCode.D))
             rigidbody.MovePosition (rigidbody.position - Vector3.right * speed * Time.deltaTime);
         if (Input.GetKey (KeyCode.A))
             rigidbody.MovePosition (rigidbody.position + Vector3.right * speed * Time.deltaTime);
     }
 
     //This section is for syncing the players and minimizing the effects of latency
     //Interpolation
     private float lastSyncronizationTime = 0f;
     private float syncDelay = 0f;
     private float syncTime = 0f;
     private Vector3 syncStartPosition = Vector3.zero;
     private Vector3 syncEndPosition = Vector3.zero;
 
     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
     {
         Vector3 syncPosition = Vector3.zero;
         Vector3 syncVelocity = Vector3.zero;
         if (stream.isWriting) 
             {
             syncPosition = rigidbody.position;
             stream.Serialize (ref syncPosition);
 
             syncVelocity = rigidbody.velocity;
             stream.Serialize(ref syncVelocity);
             } else 
                 {
             stream.Serialize(ref syncPosition);
                 
             syncTime = 0f;
             syncDelay = Time.time - lastSyncronizationTime;
             lastSyncronizationTime = Time.time;
 
             syncEndPosition = syncPosition + syncVelocity * syncDelay;
             syncStartPosition = rigidbody.position;
 
                 }
     }
 
     private void SyncedMovement()
     {
         syncTime += Time.deltaTime;
         rigidbody.position = Vector3.Lerp (syncStartPosition, syncEndPosition, syncTime / syncDelay);
     }
 
 
 }
 




Here is an image to demonstrate my scene: alt text

networkissueimage.png (514.2 kB)
Comment
Add comment · Show 1
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
avatar image Joshua4missions · Aug 08, 2014 at 06:51 PM 0
Share

When a new player joins the player count does not go up. Is the Spawn player $$anonymous$$ethod even being run by additional players?

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Network Instantiate error even when connected? 1 Answer

Network.Instantiate spawns multiple objects 2 Answers

Network.Instantiate only Instantiates on one client. 0 Answers

Network.instantiate only works for Server to client 0 Answers

Instantiate a random item at a random position 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