Created
February 28, 2022 17:08
-
-
Save foopis23/2d05c1d779031071fac700433a73dcf8 to your computer and use it in GitHub Desktop.
VRChat Send Events to Specific Players
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExampleUsage : UdonSharpBehaviour { | |
public void OnGameFinished() { | |
// determine the winner somehow up here | |
VRCSDKBaseVRCPlayerApi wiwnner = ... | |
// find the winner and run the teleport command on them | |
for (var i = 0; i < networkManager.playerIds.Length; i++) | |
{ | |
if (networkManager.playerIds[i] == winner.playerId) { | |
networkManager.clients[i].SendCustomNetworkEvent(NetworkEventTarget.Owner, "TeleportPlayerToSpawn"); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UdonSharp; | |
using UnityEngine; | |
using VRC.SDKBase; | |
public class NetworkClient : UdonSharpBehaviour | |
{ | |
public Transform spawnPoint; | |
// event to run for specific player | |
public void TeleportPlayerToSpawn() | |
{ | |
Networking.GetOwner(gameObject).TeleportTo(spawnPoint.position, spawnPoint.rotation); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UdonSharp; | |
using UnityEngine; | |
using VRC.SDKBase; | |
public class NetworkManager : UdonSharpBehaviour | |
{ | |
public int[] playerIds; | |
[SerializeField] public NetworkClient[] clients; | |
void Start() | |
{ | |
playerIds = new int[16]; | |
for (var i = 0; i < playerIds.Length; i++) | |
{ | |
playerIds[i] = -1; | |
} | |
} | |
public override void OnPlayerJoined(VRCPlayerApi player) | |
{ | |
if (!Networking.IsOwner(gameObject)) return; | |
for (var i = 0; i < playerIds.Length; i++) | |
{ | |
if (playerIds[i] != -1) continue; | |
clients[i].gameObject.SetActive(true); | |
playerIds[i] = player.playerId; | |
Networking.SetOwner(player, clients[i].gameObject); | |
break; | |
} | |
} | |
public override void OnPlayerLeft(VRCPlayerApi player) | |
{ | |
if (!Networking.IsOwner(gameObject)) return; | |
for (var i = 0; i < playerIds.Length; i++) | |
{ | |
if (playerIds[i] != player.playerId) continue; | |
clients[i].gameObject.SetActive(false); | |
playerIds[i] = -1; | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Potential Improvements: