From f360e5070c96e460c2f3a71123b7afe919464175 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 17 Mar 2019 14:07:24 +0100 Subject: [PATCH] pong ball velocity only starts when both players are connected --- Assets/Mirror/Examples/Pong/Scripts/Ball.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Assets/Mirror/Examples/Pong/Scripts/Ball.cs b/Assets/Mirror/Examples/Pong/Scripts/Ball.cs index a4fd896f9..b06bc7dda 100644 --- a/Assets/Mirror/Examples/Pong/Scripts/Ball.cs +++ b/Assets/Mirror/Examples/Pong/Scripts/Ball.cs @@ -10,9 +10,24 @@ public override void OnStartServer() { // only simulate ball physics on server GetComponent().simulated = true; + } - // Initial Velocity - GetComponent().velocity = Vector2.right * speed; + [ServerCallback] + void Update() + { + Rigidbody2D rb = GetComponent(); + + // 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; + } } float HitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)