Quantcast
Channel: Questions in topic: "spawn points"
Viewing all articles
Browse latest Browse all 102

Spawn player prefab on sockets c#

$
0
0
i have try to implement the client server to my game the server and client work perfect now i finish my player prefab and i try to spawn the player we is a server and wen is a client i have no erros but then the spawn dont work , i maybe miss something. there is my code using UnityEngine; using System.Collections; public class Menu : MonoBehaviour { public GameObject Player; public float spawnTime = 3f; public Transform[] spawnPoints; public string IP = "127.0.0.1"; public int Port = 25001; public GameObject target; public string username = ""; bool RegisterUI = false; bool LoginUI = false; void OnGUI() { if(Network.peerType == NetworkPeerType.Disconnected) { if(GUI.Button(new Rect(100,100,100,25),"Start Client")) { Network.Connect(IP,Port); } if(GUI.Button(new Rect(100,125,100,25),"Start Server")) { Network.InitializeServer(10,Port); } } else { if(Network.peerType == NetworkPeerType.Client) { if(RegisterUI == true && LoginUI == false) { username = GUI.TextArea(new Rect(100,125,110,25),username); if(GUI.Button(new Rect(100,150,110,25),"Register")) { GetComponent().RPC("Register",RPCMode.Server,username); RegisterUI = false; } } else if(LoginUI == true && RegisterUI == false) { username = GUI.TextArea(new Rect(100,125,110,25),username); if(GUI.Button(new Rect(100,150,110,25),"Login")) { GetComponent().RPC("Login",RPCMode.Server,username); } } else { GUI.Label(new Rect(100,100,100,25),"Client"); if(GUI.Button(new Rect(100,125,110,25),"Login")) { LoginUI = true; } if(GUI.Button(new Rect(100,150,110,25),"Register")) { RegisterUI = true; } if(GUI.Button(new Rect(100,175,110,25),"Logout")) { Network.Disconnect(250); } } } if(Network.peerType == NetworkPeerType.Server) { GUI.Label(new Rect(100,100,100,25),"Server"); GUI.Label(new Rect(100,125,100,25),"Connections: " + Network.connections.Length); if(GUI.Button(new Rect(100,150,100,25),"Logout")) { Network.Disconnect(250); } } } } [RPC] void Login(string Username) { if(Network.isServer) { bool checkUsername = PlayerPrefs.HasKey(Username); if(checkUsername == true) { GetComponent().RPC("LoadLevel",RPCMode.Others); } } } [RPC] void LoadLevel() { if(Network.isClient) { // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time. InvokeRepeating ("Spawn", spawnTime, spawnTime); //if(Application.loadedLevel == 0) //{ //Application.LoadLevel(0); //} } } [RPC] void Register(string Username) { if(Network.isServer) { // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time. InvokeRepeating ("Spawn", spawnTime, spawnTime); PlayerPrefs.SetString(Username,Username); } } void Spawn () { // Find a random index between zero and one less than the number of spawn points. int spawnPointIndex = Random.Range (0, spawnPoints.Length); // Create an instance of the player prefab at the randomly selected spawn point's position and rotation. Instantiate (Player, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation); } }

Viewing all articles
Browse latest Browse all 102

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>