I’m making a game of tag so I’m looking for how to make one of the player (randomly) be who will tag someone and who will run. Use C#. Please say the code, I am using photon pun 2
2 Answers
2we are people not AIs ask microsoft copilot that exact same prompt and you will get the answer:
Generated by microsoft copilot:
Certainly! To randomly assign roles to players in your game of tag using Photon PUN 2, you can follow these steps:
- Assign Player Numbers:
- In your game, each player needs a unique identifier. You can use Photon’s built-in
PlayerNumberingscript to assign a player number to each player. This script automatically assigns numbers based on the order players join the room. - Make sure you have the
PlayerNumberingscript in your project (it’s part of the Photon PUN 2 package). - Use
Player.ActorNumberto get the assigned player number for each player.
- Randomly Assign Roles:
- When the game starts (e.g., in the
OnJoinedRoomcallback), the Master Client can randomly assign roles (e.g., tagger or runner) to each player. - You can use
Random.Rangeto generate a random number (e.g., 0 or 1) to determine the role. - Send this role information to all players using Photon’s
RaiseEventmethod.
- Example Code:
C#
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
public class TagGameController : MonoBehaviourPunCallbacks
{
private const int TaggerRole = 0;
private const int RunnerRole = 1;
public override void OnJoinedRoom()
{
base.OnJoinedRoom();
// Check if we are the Master Client
if (PhotonNetwork.IsMasterClient)
{
// Get the list of players in the room
foreach (var player in PhotonNetwork.PlayerList)
{
// Generate a random role (tagger or runner)
int randomRole = Random.Range(0, 2);
// Assign the role to the player
player.SetCustomProperties(new Hashtable { { "Role", randomRole } });
// Log the role for debugging
Debug.Log($"Player {player.ActorNumber} is {(randomRole == TaggerRole ? "Tagger" : "Runner")}");
}
}
}
}
AI-generated code. Review and use carefully. More info on FAQ.
- In this example, we use custom properties to store the player’s role (0 for tagger, 1 for runner).
- Adjust the role assignment logic according to your game’s requirements.
Remember to adapt this code to your specific game mechanics and integrate it with your existing Photon PUN 2 setup. Good luck with your game of tag! ![]()
![]()
![]()
It’s because I am a new programmer. I started programing 8 months ago. But thanks!
This is less of a question and more of a prompt text asking to generate code for you
– PixelEyesDev