我创建了一个简单的纸牌游戏来测试使用 Photon 和 Unity。在游戏中,每个玩家都有一张牌,他们只需点击一个“移动空间”,这张牌就会移动到那里。玩家 1 只能在他们的回合中移动他们的牌,反之亦然,并且每当一张牌移动时,回合就会改变。
问题是,虽然玩家 1 会移动他们的卡 - 并且它会在两个版本的游戏中同步 - 当涉及到玩家 2 时,他们无法移动他们的卡,玩家 1 也不能再移动任何东西。有时玩家 2 可以移动他们的卡片,但游戏总是会冻结或无法让任何人移动任何东西。
以下是我的游戏逻辑代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
public class Game : MonoBehaviour
{
PhotonView view;
public static bool p1Turn = true;
public bool player1 = false;
public bool player2 = false;
public GameObject p1_card, p2_card, moveSpace;
public static Vector3 reference = new Vector3(0f, 0f, 0f);
public static Vector3 currentMoveCoords = reference;
public void Update()
{
Move();
}
public void Start()
{
view = GetComponent<PhotonView>();
SpawnMoveSpace(moveSpace, -7f, -2f);
SpawnMoveSpace(moveSpace, 0f, -2f);
SpawnMoveSpace(moveSpace, 7f, -2f);
SpawnMoveSpace(moveSpace, -7f, 2f);
SpawnMoveSpace(moveSpace, 0f, 2f);
SpawnMoveSpace(moveSpace, 7f, 2f);
if(PhotonNetwork.CurrentRoom.PlayerCount == 1)
{
GameObject p1 = SpawnCard(p1_card, -7f, -2f);
this.player1 = true;
}
else
{
GameObject p2 = SpawnCard(p2_card, 0f, -2f);
this.player2 = true;
}
}
public GameObject SpawnCard(GameObject obj, float x, float y)
{
PhotonNetwork.Instantiate(obj.name, new Vector3(x, y, -2), Quaternion.identity);
return obj;
}
public GameObject SpawnMoveSpace(GameObject obj, float x, float y)
{
Instantiate(obj, new Vector3(x, y, 0), Quaternion.identity);
return obj;
}
public void Move()
{
view.RPC("MoveRPC", RpcTarget.All);
}
[PunRPC]
void MoveRPC()
{
if(currentMoveCoords != reference)
{
if(p1Turn && this.player1)
{
GameObject[] objects = GameObject.FindGameObjectsWithTag("p1");
foreach(GameObject obj in objects)
{
obj.transform.position = currentMoveCoords;
}
p1Turn = false;
}
else if(p1Turn == false && this.player2)
{
GameObject[] objects = GameObject.FindGameObjectsWithTag("p2");
foreach(GameObject obj in objects)
{
obj.transform.position = currentMoveCoords;
}
p1Turn = true;
}
currentMoveCoords = reference;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveSpace : MonoBehaviour
{
void OnMouseDown()
{
Game.currentMoveCoords = this.transform.position;
}
}
我已将 PhotonView 和 Photon Transform View Classic 组件附加到 p1 和 p2 卡上。任何帮助将不胜感激。