I am building a Runnig Race Multiplayer Game in Unity3D using UNET. I have 2 Player running straight in a game Subway Surfer. I want to update position of the player while running who is first and who is second and vice versa, the code is working for Host Player but not updating other player postion. Please help me what i am doing wrong.
Here is my code:
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");
}
}
}
}