我正在使用 UNET 在 Unity3D 中构建 Runnig Race 多人游戏。我有 2 名玩家在游戏 Subway Surfer 中直接奔跑。我想在运行谁是第一谁是第二时更新玩家的位置,反之亦然,该代码适用于 Host Player,但不更新其他玩家的位置。请帮助我做错了什么。
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerPosition : NetworkBehaviour {
GameObject[] Players;
Vector3 playerPos;
void Start () {
InvokeRepeating("UpdatePosition", 0.5f, 0.5f);
}
void UpdatePosition () {
if (!isLocalPlayer)
return;
ClientPositionCalls();
}
[Client]
void ClientPositionCalls()
{
CmdServerPosition();
}
[Command]
public void CmdServerPosition()
{
Position();
}
[Server]
public void Position()
{
playerPos = transform.position;
RpcPosition(playerPos);
}
[ClientRpc]
void RpcPosition(Vector3 pos)
{
if (isLocalPlayer)
{
playerPos = pos;
Players = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject p in Players)
{
if (p.transform.position.z < pos.z)
PlayerCanvas.canvas.WritePositionText("1");
else
PlayerCanvas.canvas.WritePositionText("2");
}
}
}
}