简体   繁体   中英

Unity Photon PUN RPC call

I have a question about RPC and Photon PUN.

I have 1 GameObject in my game that calls an RPC method. This game object contains a C# script (that contains the RPC method) and the PhotonView component with/without (is should be the same) the observed script and the ObserveOption equals to Off.

I have a character which have to shoot to a cube. This cube has the previously described script and component attach to it.

The RPC is sent once (when the cube has been collided with a projectile) but in my scene I have many Cube which should receive the message because I have to decrease the health of the hit cube. The problem is that only 1 Cube receive the RPC call (The RPC is caught by the first instantiated object at the start of the game) but the RPC should be received by all the cube (is that right?) in my scene.

The cubes are exactly the same but they have a different photonView ID (rightly) so we could check whether the hit cube to which decrease the health is the right one.

This is the significant part of the code:

public static void reduceHealthRPC(float damage, int viewID)    
{         
    photonView.RPC("reduceHealth", PhotonTargets.All, damage, viewID);    
}

[RPC]
public void reduceHealth(float damage, int viewID)
{
    if(this._viewID != viewID) 
    {
        Debug.Log ("The view ID is not mine. My viewID is: " + _viewID + ", and the one which is coming form RPC is: " + viewID);
        return;
    }

    currentHealth -= damage;
    Debug.Log("My life is: " + currentHealth);
}

Please help me to unerstand what I'm doing wrong. Thank you.

If you need call RPC for all cubes, do it for every cube. Currently you are calling RPC only for object referenced by static 'photonView' variable. Depending on how it's initialized, RPC called on that object.

我可能会迟到,但请尝试PhotonTargets.AllBuffered ,这将使 RPC 调用在稍后加入的玩家上运行。

Might be super late but have you tried this? cube.cs is attached to all cubes. in main.cs

cube[] allCubes = FindObjectsOfType<cube>();

for (int x = 0; x < allCubes.length; x++)
allCubes[x].reduceHealthRPC(dam,id);

this will send the code to all cubes with one target id, if this is your goal but you are much better to just rpc on the individual cube.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM