我正在为我的学校项目联网一个 2d 游戏,但在尝试让玩家在网络场景中“进化”时遇到了一个问题。玩家只会在客户端而不是主机上正确进化。导致问题的代码在 Evolve 脚本中的某处,但我不知道如何通过代码显示问题,因为问题在于代码的制定。因此,我附上了这两个代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine;
public class Evolve : NetworkBehaviour {
public bool canEvolve;
public bool isEvolving;
public int evoTimer;
public int timeToEvolve;
public int currentEvo;
public GameObject Evo0;
public GameObject Evo1;
public GameObject Evo2;
public GameObject nextEvo;
public GameObject nextA_Atk;
public GameObject nextB_Atk;
// Use this for initialization
void Start() {
canEvolve = true;
isEvolving = false;
evoTimer = 0;
timeToEvolve = GameObject.FindGameObjectWithTag("Settings").GetComponent<GameSettings>().timeToEvolve;
currentEvo = -1;
RpcCallEvolve();
}
// Update is called once per frame
void Update() {
if (!isLocalPlayer)
{
return;
}
if ((Input.GetButton("Evolve")) && (canEvolve == true))
{
isEvolving = true;
evoTimer += 1;
if (evoTimer >= timeToEvolve)
{
RpcCallEvolve();
}
}
else
{
isEvolving = false;
evoTimer = 0;
}
}
[ClientRpc(channel = 0)]
void RpcCallEvolve()
{
currentEvo += 1;
switch (currentEvo)
{
case 0:
nextEvo = Instantiate(Evo0) as GameObject;
break;
case 1:
nextEvo = Instantiate(Evo1) as GameObject;
break;
case 2:
nextEvo = Instantiate(Evo2) as GameObject;
break;
case 3:
Win(name);
break;
}
GetComponent<SpriteRenderer>().sprite = nextEvo.GetComponent<SpriteRenderer>().sprite;
GetComponent<PolygonCollider2D>().points = nextEvo.GetComponent<PolygonCollider2D>().points;
canEvolve = true;
evoTimer = 0;
Destroy(nextEvo);
}
void Win(string player)
{
Debug.Log("Winner is " + player);
gameObject.SetActive(false);
}
}
如果可以帮助人们找出问题,还有一个下载完整项目的链接。https://1drv.ms/u/s!ArLjF5CAV1VwgbxZdCQkb_9PZ_j5kw
我不需要修复或整理其余代码,只需了解可能对上述问题有用的信息。
非常感谢任何提供帮助的人。