Skip to content

Commit

Permalink
Better mulitplayer connect mid game not starting scene and initial co…
Browse files Browse the repository at this point in the history
…nnection orientation, temp only 1 rock per level.
  • Loading branch information
plaidpants committed Feb 1, 2022
1 parent defca77 commit 096e9d0
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 49 deletions.
22 changes: 19 additions & 3 deletions Assets/NetworkDiscoveryHUDAutoConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class NetworkDiscoveryHUDAutoConnect : MonoBehaviour

public NetworkDiscovery networkDiscovery;

bool triedConnect = false;

void tryConnect()
{
// try all found servers
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -64,7 +79,8 @@ void OnGUI()
return;

if (!NetworkClient.isConnected && !NetworkServer.active && !NetworkClient.active)
DrawGUI();
return;
//DrawGUI();
}

void DrawGUI()
Expand Down
3 changes: 0 additions & 3 deletions Assets/NetworkManagerRocket.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Binary file modified Assets/RockField1.prefab
Binary file not shown.
Binary file modified Assets/RockField2.prefab
Binary file not shown.
45 changes: 11 additions & 34 deletions Assets/RockSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Rigidbody>();
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)
{
Expand Down
Binary file modified Assets/RockSphere1.prefab
Binary file not shown.
33 changes: 24 additions & 9 deletions Assets/RocketSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Renderer>().material is used.
Expand All @@ -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
Expand All @@ -71,10 +69,10 @@ public override void OnStartServer()
RocketSphere[] players = FindObjectsOfType<RocketSphere>();

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
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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;
}
}
Expand Down
Binary file modified ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file modified ProjectSettings/ProjectSettings.asset
Binary file not shown.

0 comments on commit 096e9d0

Please sign in to comment.