diff --git a/Assets/NetworkDiscoveryHUDAutoConnect.cs b/Assets/NetworkDiscoveryHUDAutoConnect.cs index 43ba3a2..229df15 100644 --- a/Assets/NetworkDiscoveryHUDAutoConnect.cs +++ b/Assets/NetworkDiscoveryHUDAutoConnect.cs @@ -15,6 +15,8 @@ public class NetworkDiscoveryHUDAutoConnect : MonoBehaviour public NetworkDiscovery networkDiscovery; + bool triedConnect = false; + void tryConnect() { // try all found servers @@ -33,14 +35,17 @@ void tryConnect() NetworkManager.singleton.StartHost(); networkDiscovery.AdvertiseServer(); } + + // flag that we tried + triedConnect = true; } void Start() { discoveredServers.Clear(); networkDiscovery.StartDiscovery(); - // wait 5 seconds before trying to connect so we find any servers out there already - Invoke("tryConnect", 5); + // wait 2 + random seconds before trying to connect so we find any servers out there already + Invoke("tryConnect", 2 + (int)UnityEngine.Random.Range(0,4)); } #if UNITY_EDITOR @@ -55,6 +60,16 @@ void OnValidate() } #endif + void Update() + { + // after we tried to connect, if we lose the connection, shutdown the app and allow the user to restart it. + if (triedConnect && !NetworkClient.isConnected && !NetworkServer.active && !NetworkClient.active) + { + // lost the connection, need to restart the app + Application.Quit(); + } + } + void OnGUI() { if (NetworkManager.singleton == null) @@ -64,7 +79,8 @@ void OnGUI() return; if (!NetworkClient.isConnected && !NetworkServer.active && !NetworkClient.active) - DrawGUI(); + return; + //DrawGUI(); } void DrawGUI() diff --git a/Assets/NetworkManagerRocket.cs b/Assets/NetworkManagerRocket.cs index eedad64..4f70120 100644 --- a/Assets/NetworkManagerRocket.cs +++ b/Assets/NetworkManagerRocket.cs @@ -1,9 +1,6 @@ using Mirror; using UnityEngine; -// 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). [AddComponentMenu("")] public class NetworkManagerRocket : NetworkManager { diff --git a/Assets/RockField1.prefab b/Assets/RockField1.prefab index d48bef1..36679a1 100644 Binary files a/Assets/RockField1.prefab and b/Assets/RockField1.prefab differ diff --git a/Assets/RockField2.prefab b/Assets/RockField2.prefab index c2589a0..75d754f 100644 Binary files a/Assets/RockField2.prefab and b/Assets/RockField2.prefab differ diff --git a/Assets/RockSphere.cs b/Assets/RockSphere.cs index fcc2b5f..74ab8f9 100644 --- a/Assets/RockSphere.cs +++ b/Assets/RockSphere.cs @@ -26,6 +26,8 @@ public override void OnStartServer() // call the base function, probably is empty base.OnStartServer(); + // only do the final rock positioning for the rock on the server use syncvars to sync with the clients + // get the current radius and position from parent gameobject radius = transform.position.magnitude; pos = transform.position; @@ -51,43 +53,18 @@ public override void OnStartServer() void Start() { - // only do the final rock positioning for the rock on the server use syncvars to sync with the clients - if (false)//isServer) - { - // get the current radius and position from parent gameobject - radius = transform.position.magnitude; - pos = transform.position; - - // get the current rotation from the parent position - rot = Quaternion.FromToRotation(Vector3.forward, pos); - - // reset the parent gameobject position back to the center - transform.position = Vector3.zero; - transform.rotation = Quaternion.identity; - - // move the child rock to original location and rotation - rock = transform.Find("Rock.old").gameObject; - rock.transform.position = pos; - rock.transform.rotation = rot; - - // apply some rotational torque to the parent gameobject object with the rock attached as a child - Rigidbody rb = GetComponent(); - Vector3 torque = Random.onUnitSphere * (Random.Range(minSpeed, maxSpeed) / radius); - // ???? note this could be pointing right at us or away so no torque would be added need to catch this and get a new torque - rb.AddTorque(Vector3.Cross(torque, pos.normalized)); - } - else - { - // move the child rock to original location and rotation from the syncvars since mirror will not do this for us - // the rock has moved since creation for clients connecting mid-game, offset is in local coords - rock = transform.Find("Rock.old").gameObject; - rock.transform.localPosition = pos; - rock.transform.localRotation = rot; - } - count++; } + public override void OnStartClient() + { + // move the child rock to original location and rotation from the syncvars since mirror will not do this for us + // the rock has moved since creation for clients connecting mid-game, offset is in local coords + rock = transform.Find("Rock.old").gameObject; + rock.transform.localPosition = pos; + rock.transform.localRotation = rot; + } + [ServerCallback] void OnTriggerEnter(Collider other) { diff --git a/Assets/RockSphere1.prefab b/Assets/RockSphere1.prefab index 93e420c..705b1a4 100644 Binary files a/Assets/RockSphere1.prefab and b/Assets/RockSphere1.prefab differ diff --git a/Assets/RocketSphere.cs b/Assets/RocketSphere.cs index 23e7c9d..1648300 100644 --- a/Assets/RocketSphere.cs +++ b/Assets/RocketSphere.cs @@ -45,8 +45,6 @@ public class RocketSphere : NetworkBehaviour [SyncVar] Color RocketColor = Color.white; [SyncVar] float hue = 0.0f; [SyncVar] bool visible = false; - // [SyncVar] Vector3 posSave = Vector3.zero; - // [SyncVar] Quaternion rotSave = Quaternion.identity; [SyncVar] Quaternion rot2Save = Quaternion.identity; // Unity makes a clone of the Material every time GetComponent().material is used. @@ -60,7 +58,7 @@ void OnDestroy() public override void OnStartServer() { - // call the base function, probably is empty + // call the base function, important, odd behavior when connecting when not on same start scene base.OnStartServer(); // create a random color for each player as they are created on the server @@ -71,10 +69,10 @@ public override void OnStartServer() RocketSphere[] players = FindObjectsOfType(); int count = 0; - for (int i = 1; i < players.Length; i++) + for (int i = 0; i < players.Length; i++) { count++; - if (count > 10) + if (count > 30) { Debug.Log("Give up finding a better color"); break; // give up @@ -84,7 +82,7 @@ public override void OnStartServer() // difference check must be small enough that we have enough colors for all players. // if max player count is increased from 4, this value should be reassessed // this could be an infinte check otherwise, might want a failsafe escape - if ((Mathf.Abs(hue - players[i].hue) < 0.15f) && (players[i] != this)) + if ((Mathf.Abs(hue - players[i].hue) < 0.2f) && (players[i] != this)) { // generate a new hue if it is too close to this other players color hue = Random.Range(0.0f, 1.0f); @@ -96,11 +94,30 @@ public override void OnStartServer() // rocket color is now set for this player until it is destroyed or disconnects RocketColor = Color.HSVToRGB(hue, 1.0f, 1.0f); + + // Find the rocket child object + rocket = transform.Find("Rocket").gameObject; + + //reset the position back to the center + transform.position = Vector3.zero; + transform.rotation = Quaternion.identity; + + // Move rocket child gameobject out to radius + rocket.transform.position = Vector3.forward * radius; + rocket.transform.rotation = Quaternion.FromToRotation(Vector3.forward, rocket.transform.position); } // Use this for initialization void Start() { + + } + + public override void OnStartClient() + { + // call the base function, important, odd behavior when connecting when not on same start scene + base.OnStartClient(); + // Find the rocket child object rocket = transform.Find("Rocket").gameObject; @@ -121,7 +138,7 @@ void Start() // Move rocket child gameobject out to radius rocket.transform.position = Vector3.forward * radius; - rocket.transform.rotation = Quaternion.FromToRotation(Vector3.forward, rocket.transform.position); ; + rocket.transform.rotation = Quaternion.FromToRotation(Vector3.forward, rocket.transform.position); // Keep the player objects through level changes DontDestroyOnLoad(this); @@ -145,8 +162,6 @@ void Start() { // not our rocket so we need to update the state based on the server sync var state rocket.SetActive(visible); - //rocket.transform.position = posSave; - //rocket.transform.rotation = rotSave; transform.rotation = rot2Save; } } diff --git a/ProjectSettings/GraphicsSettings.asset b/ProjectSettings/GraphicsSettings.asset index 9861984..4794c37 100644 Binary files a/ProjectSettings/GraphicsSettings.asset and b/ProjectSettings/GraphicsSettings.asset differ diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 261231a..3b68d66 100644 Binary files a/ProjectSettings/ProjectSettings.asset and b/ProjectSettings/ProjectSettings.asset differ