- Home /
Player Spawn point not changing
Hey everyone. I am getting my feet wet in networking now and I cannot get different spawn points for when a player joins the game it keeps going back to the same spawn point. My player count variable does not change or it somehow gets reset in the code or something to it wont increment.
Here is what I got it work but does not change spawn points. Thanks for any help with this.
var remoteIP = ""; //ip of comp tring to connect to
var remotePort = 25000;
var listenPort = 25000;
var useNAT = false;
var yourIP = ""; //server address
var yourPort = ""; // port
var playerCount : int;
public var spawnPoints : Transform[];
//player and jump master spawn componets
var playerPrefab : GameObject;
//spawn points
var sp1: Transform;
var sp2: Transform;
//camera stuff
private var cam : Camera;
public var skyBoxMat : Material;
var camHarness : GameObject;
function Start () {
playerCount = 0;
}
function OnGUI()
{
if(Network.peerType == NetworkPeerType.Disconnected)
{
//connect to server
if(GUI.Button(Rect(10, 10, 100, 30), "Connect"))
{
Network.useNat = useNAT;
Network.Connect(remoteIP, remotePort);
}
//creat own server
if(GUI.Button(Rect(10, 50, 100, 30), "Start Server" ))
{
Network.useNat = useNAT;
//first number is max connections
Network.InitializeServer(14,listenPort );
//loop though every object in the game
for(var go : GameObject in FindObjectsOfType(GameObject))
{
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
}
//use these for accsessing the server that not hard coded
remoteIP = GUI.TextField(Rect(120, 10, 100, 20), remoteIP);
remotePort = parseInt(GUI.TextField(Rect(230, 10, 40, 20), remotePort.ToString()));
}else
{
//if player is connected to the game
var ipAddress = Network.player.ipAddress;
var port = Network.player.port.ToString();
//see ip and port for connected player
GUI.Label(Rect(140, 20, 250, 40), "IP Address: " + ipAddress + " port : " + port);
if(GUI.Button(Rect(10,10,100, 50), "Disconnect"))
{
Network.Disconnect(200);
}
}
}
//spwan the master jumper
function OnServerInitialized()
{
Debug.Log("Server initilized");
spawnPlayer();
playerCount++;
}
function OnConnectedToServer()
{
//loop though every object in the game
for(var go : GameObject in FindObjectsOfType(GameObject))
{
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
spawnPlayer();
playerCount++;
}
//spawn player prefab
function spawnPlayer()
{
//Network.Instantiate(playerPrefab, sp1.position, Quaternion.identity, 0);
Debug.Log("Player Count" + playerCount);
if(playerCount == 0)
{
Network.Instantiate(playerPrefab, sp1.position, Quaternion.identity, 0);
}
if(playerCount == 1)
{
Network.Instantiate(playerPrefab, sp2.position, Quaternion.identity, 0);
}
//Network.Instantiate(playerPrefab, sp1.position, Quaternion.identity, 0);
//Network.Instantiate(playerPrefab, spawnPoints[playerCount].position, Quaternion.identity, 0);
playerCount++;
camHarness = GameObject.Find("CamHarness");
cam = gameObject.AddComponent(Camera);
var rot : Quaternion;
rot.x = camHarness.transform.rotation.x;
rot.y = camHarness.transform.rotation.y;
rot.z = camHarness.transform.rotation.z;
rot.w = camHarness.transform.rotation.w;
cam.transform.position = camHarness.transform.position;
cam.transform.rotation = rot;
cam.transform.parent = camHarness.transform;
//set near and far view for camera
cam.near = 0.1f;
cam.far = 35000;
RenderSettings.skybox = skyBoxMat;
}
Answer by danilonishimura · Jan 17, 2013 at 10:23 PM
When you connect to a server, you need to spawn the other players before spawning you, otherwise, when you enter the room, you'll always be the first, and therefore, always spawn in the zero position.
Updated:
Reading more carefully, I saw that you're not listening to the OnPlayerConnected event.
Here is the method you should implement on your code:
function OnPlayerConnected(networkPlayer:NetworkPlayer):void
{
Debug.Log (networkPlayer.guid + " connected");
spawnPlayer();
}
That make sense to me but I fail to see how to do that
Thank you I think I got it working. well the player count is going up so now I just have to edit where they spawn. Thanks again!
hi.. getting same problem... I have spawned player successfully :) But trying to display own player on screen centre position and other joined player should be available either left or right. Each player can see his position on center in his app. Is it possible ? I am using photonview.is$$anonymous$$e method. But not succeed.
please help
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Network Transform Component forcing Object to a specific position + rotation 0 Answers
[UNET] Dynamically position NetworkStartPosition Gameobjects 2 Answers
NetworkSpawnpoint not working 0 Answers
Having trouble randomizing spawns on Tanks! Networked from asset store 0 Answers