1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-27 00:54:20 +00:00
better-tee/Assets/Packages/Mirror/Examples/Pong/Scripts/NetworkManagerPong.cs
2019-09-16 00:28:36 +02:00

37 lines
1.3 KiB
C#

using UnityEngine;
using Mirror;
// Custom NetworkManager that simply assigns the correct racket positions when
// spawning players. The built in RoundRobin spawn method wouldn't work after
// someone reconnects (both players would be on the same side).
public class NetworkManagerPong : NetworkManager
{
public Transform leftRacketSpawn;
public Transform rightRacketSpawn;
GameObject ball;
public override void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage)
{
// add player at correct spawn position
Transform start = numPlayers == 0 ? leftRacketSpawn : rightRacketSpawn;
GameObject player = Instantiate(playerPrefab, start.position, start.rotation);
NetworkServer.AddPlayerForConnection(conn, player);
// spawn ball if two players
if (numPlayers == 2)
{
ball = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "Ball"));
NetworkServer.Spawn(ball);
}
}
public override void OnServerDisconnect(NetworkConnection conn)
{
// destroy ball
if (ball != null)
NetworkServer.Destroy(ball);
// call base functionality (actually destroys the player)
base.OnServerDisconnect(conn);
}
}