admin管理员组

文章数量:1125925

I am developing a multiplayer video game in Unity and Photon PUN 2 where each player has a dice with their avatar, when starting the game I have disabled the dice for all players. When the game starts, the first player's turn is executed, activating his dice. When the first player rolls and the dice fall, the dice will be hidden and the next player's will be activated, but I cannot get the dice to be shown to all players.

Could someone tell me what I need to be able to activate the GameObject of any player and so that the owner of the GameObject can only move them on their turn.

The code that I tried to make shows me the data only for my screen but not on the other players' screens.

using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using TMPro;
using System.Collections;

public class Launcher : MonoBehaviourPunCallbacks
{
    public PhotonView playerPrefab;

    public PhotonView dadosPrefab;

    public Transform spawnPoint;

    public Transform spawnPointDados;

    public TMP_Text conteo;

    private GameObject jugador;

    private readonly GameObject[] jugadores = new GameObject[2];

    private float timeRemaining = 1f;

    private bool timerIsRunning = false;

    private int posicion = 0;

    void Start()
    {   
        conteo.text = "Esperando otros jugadores";
    }

    public override void OnJoinedRoom()
    {
        Debug.Log($"Jugador unido a la sala. Posición de spawn: {spawnPoint.position}");
        Debug.Log($"Número total de jugadores: {PhotonNetwork.CurrentRoom.PlayerCount}");
        Vector3 spawnPosition = spawnPoint.position;
        Vector3 spawnPositionDados = spawnPointDados.position;
        jugador = PhotonNetwork.Instantiate(playerPrefab.name, spawnPosition, Quaternion.identity);
        PhotonNetwork.Instantiate(dadosPrefab.name, spawnPositionDados, Quaternion.identity);
        Player[] players = PhotonNetwork.PlayerList;
        Player miJugador = PhotonNetwork.LocalPlayer;
        int posicion = 3;
        for (int i = players.Length - 1; i >= 0; i--)
        {
            var player = players[i];
            if (player.ActorNumber == miJugador.ActorNumber)
            {
                AgregarJugador(this.posicion, player.NickName, i);
                this.posicion++;
            }
            else
            {
                AgregarJugador(posicion, player.NickName, i);
                posicion--;
            }
        }
    }

    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        var i = newPlayer.ActorNumber - 1;
        AgregarJugador(posicion, newPlayer.NickName, i);
        posicion++;
    }

    private void AgregarJugador(int posicion, string name, int indice)
    {
        Debug.Log($"Jugador: {name}, Posicion: {posicion + 1}");
        jugador.GetComponent<PhotonView>().RPC("SetJugador", RpcTarget.AllBuffered, posicion, name);
        jugadores[indice] = jugador;
        if (PhotonNetwork.CurrentRoom.PlayerCount == jugadores.Length && !timerIsRunning)
        {
            photonView.RPC("StartCountdown", RpcTarget.AllBuffered);
        }
    }

    [PunRPC]
    public void IniciarTurno(int nuevoJugador)
    {
        Debug.Log($"Iniciando turno del jugador {nuevoJugador}");
        var jugador = jugadores[nuevoJugador];
        //jugador.transform.Find("Dados").gameObject.SetActive(true);

    }

    [PunRPC]
    private void ActualizarDadosVisibles(int jugadorActual)
    {
        for (int i = 0; i < jugadores.Length; i++)
        {
            if (jugadores[i] != null)
            {
                Transform dados = jugadores[i].transform.Find("Dados");
                if (dados != null)
                {
                    dados.gameObject.SetActive(i == jugadorActual);
                }
            }
        }
    }

    [PunRPC]
    private void StartCountdown()
    {
        if (!timerIsRunning)
        {
            timerIsRunning = true;
            StartCoroutine(CountdownCoroutine());
        }
    }

    private IEnumerator CountdownCoroutine()
    {
        while (timeRemaining > 0)
        {
            conteo.text = $"Comenzando en {timeRemaining}";
            yield return new WaitForSeconds(1f);
            timeRemaining--;
        }

        conteo.text = "Comienza el juego!";
        yield return new WaitForSeconds(1f);
        conteo.enabled = false;
        jugadores[0] = jugador;
        photonView.RPC("IniciarTurno", RpcTarget.AllBuffered, 0);
    }

    public void SiguienteTurno(int jugadorActual)
    {
        int siguienteJugador = (jugadorActual + 1) % jugadores.Length;
        photonView.RPC("ActualizarDadosVisibles", RpcTarget.All, siguienteJugador);
    }

}

本文标签: