- Home /
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:
networkissueimage.png
(514.2 kB)
Comment
When a new player joins the player count does not go up. Is the Spawn player $$anonymous$$ethod even being run by additional players?