pong: NetworkManagerPong handles ball spawn and despawn

This commit is contained in:
vis2k 2019-03-17 14:34:59 +01:00
parent 7e9b3efa7d
commit d72a10b4a9
3 changed files with 27 additions and 82 deletions

View File

@ -219,7 +219,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &753891880
GameObject:
@ -327,67 +327,6 @@ BoxCollider2D:
serializedVersion: 2
m_Size: {x: 50, y: 1}
m_EdgeRadius: 0
--- !u!1001 &1140775523
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4700925592147096, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
propertyPath: m_RootOrder
value: 3
objectReference: {fileID: 0}
- target: {fileID: 114290021321007948, guid: 5f7a7f34494ed40268eff49dbf9168bf,
type: 3}
propertyPath: m_SceneId
value: 14060562
objectReference: {fileID: 0}
- target: {fileID: 50354248948880112, guid: 5f7a7f34494ed40268eff49dbf9168bf,
type: 3}
propertyPath: m_Simulated
value: 0
objectReference: {fileID: 0}
- target: {fileID: 50354248948880112, guid: 5f7a7f34494ed40268eff49dbf9168bf,
type: 3}
propertyPath: m_CollisionDetection
value: 1
objectReference: {fileID: 0}
- target: {fileID: 50354248948880112, guid: 5f7a7f34494ed40268eff49dbf9168bf,
type: 3}
propertyPath: m_Interpolate
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
--- !u!1 &1346799726
GameObject:
m_ObjectHideFlags: 0
@ -728,7 +667,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1575697329
GameObject:
@ -945,9 +884,12 @@ MonoBehaviour:
type: 3}
autoCreatePlayer: 1
playerSpawnMethod: 1
spawnPrefabs: []
spawnPrefabs:
- {fileID: 1080679924113744, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
leftRacketSpawn: {fileID: 473997961}
rightRacketSpawn: {fileID: 1397990096}
ballPrefab: {fileID: 1080679924113744, guid: 5f7a7f34494ed40268eff49dbf9168bf, type: 3}
ball: {fileID: 0}
--- !u!114 &1886246553
MonoBehaviour:
m_ObjectHideFlags: 0

View File

@ -10,24 +10,7 @@ public override void OnStartServer()
{
// only simulate ball physics on server
GetComponent<Rigidbody2D>().simulated = true;
}
[ServerCallback]
void Update()
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
// if less than two players then reset ball
if (NetworkManager.singleton.numPlayers < 2)
{
rb.velocity = Vector2.zero;
transform.position = Vector2.zero;
}
// if two players joined, then enable velocity if not done yet
else if (NetworkManager.singleton.numPlayers == 2 && rb.velocity == Vector2.zero)
{
rb.velocity = Vector2.right * speed;
}
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
}
float HitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)

View File

@ -8,11 +8,31 @@ public class NetworkManagerPong : NetworkManager
{
public Transform leftRacketSpawn;
public Transform rightRacketSpawn;
public GameObject ballPrefab;
public 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(ballPrefab);
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);
}
}