Photon pun unity, RPC функция срабатывает несколько раз

Возникла проблема - я делаю мультиплеерную мини-игру футбола. Но проблема в том, что когда кто-то забивает мяч, гол может засчитаться 2-3 раза и из-за этого ломается пол игры. Я подозреваю, что дело тут в RPC.

public void OnCollisionEnter(Collision other)
{
    if (other.collider.CompareTag("Left Vorota"))
    {
        Debug.Log("Ball lefted");
        SendRpcLeft();
    }
    if (other.collider.CompareTag("Right Vorota"))
    {
        Debug.Log("Ball righted");
        SendRpcRight();
    }
}

void SendRpcRight()
{
    photonView.RPC("Send_Data_Right", RpcTarget.AllViaServer, count1, count2);
}
void SendRpcLeft()
{
    photonView.RPC("Send_Data_Left", RpcTarget.AllViaServer, count1, count2);
}

[PunRPC]
void Send_Data_Right(int counting1, int counting2)
{
    counting1 = counting1 + 1;
    count1 = counting1;
    finalCounting = counting1 + ":" + counting2;
    counttext.text = finalCounting;
    ReloadScene();
}

[PunRPC]
void Send_Data_Left(int counting1, int counting2)
{
    counting2 = counting2 + 1;
    count2 = counting2;
    finalCounting = counting1 + ":" + counting2;
    counttext.text = finalCounting;
    ReloadScene();
}

private void ReloadScene()
{
    PhotonNetwork.AutomaticallySyncScene = true;
    PlayerPrefs.SetString("countText", counttext.text);
    PlayerPrefs.SetInt("countValue1", count1);
    PlayerPrefs.SetInt("countValue2", count2);
    PlayerPrefsX.SetBool("RocketCanFlyRace", true);
    PhotonNetwork.LoadLevel("MPRoomSoccer");
}

Ответы (1 шт):

Автор решения: Nickolay Chistov

Решение довольно простое:

  • Определите две переменные типа bool, например CanGoalLeft и CanGoalRight
  • Далее когда забили гол устанавливаете значение false нужной переменной
  • Когда мяч выходит из ворот снова устанавливаем значение true

Чтобы это работало код нужен такой:

...
public void OnCollisionEnter(Collision other)
{
    if (other.collider.CompareTag("Left Vorota") && CanGoalLeft)
    {
        Debug.Log("Ball lefted");
        CanGoalLeft = false;
        SendRpcLeft();
    }
    if (other.collider.CompareTag("Right Vorota") && CanGoalRight)
    {
        Debug.Log("Ball righted");
        CanGoalRight = false;
        SendRpcRight();
    }
}

public void OnCollisionExit(Collision other)
{
    if (other.collider.CompareTag("Left Vorota"))
    {
        CanGoalLeft = true;
    }
    if (other.collider.CompareTag("Right Vorota"))
    {
        CanGoalRight = true;
    }
}
...
→ Ссылка