Skip to content

Instantly share code, notes, and snippets.

@SocketWeaver
Last active June 13, 2019 20:09
Show Gist options
  • Save SocketWeaver/23777453a9c4e6150977fc888d37f2d1 to your computer and use it in GitHub Desktop.
Save SocketWeaver/23777453a9c4e6150977fc888d37f2d1 to your computer and use it in GitHub Desktop.
Set the WinnerId SyncProperty to the player who completed the laps first
void OnTriggerEnter(Collider other)
{
GameObject go = other.gameObject;
NetworkID networkID = go.GetComponent<NetworkID>();
// check if the player is the local player
if (networkID.IsMine)
{
lap_ = lap_ + 1;
guiManager.SetLapRecord(lap_, lapsToWin);
if (lap_ == lapsToWin)
{
Debug.Log("Winner!!");
string winnerId = remotePropertyAgent.GetPropertyWithName(WIINER_ID).GetStringValue();
Debug.Log("OnTriggerEnter winnerID " + winnerId);
// if winnerId is empty, local player is the winner
// If the winner did not leave the game and finished the laps again, the player should still be the winner
if (string.IsNullOrEmpty(winnerId) || winnerId.Equals(NetworkClient.Instance.PlayerId))
{
remotePropertyAgent.Modify(WIINER_ID, NetworkClient.Instance.PlayerId);
guiManager.SetMainText("1st");
}
else
{
guiManager.SetMainText("2nd");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment