简体   繁体   English

使用Photon Unity Networking向联网游戏中的特定玩家显示消息

[英]Display a message to a specific player in a networked game using Photon Unity Networking

On entering a trigger zone of an object a message is displayed to the player (which is found by tag). 进入对象的触发区域后,会向播放器显示一条消息(通过标签找到)。 If the player then presses the pickup button, the item is added to their inventory. 如果播放器随后按下拾取按钮,则该物品将添加到其库存中。 It works fine in single-player games. 在单人游戏中效果很好。

When I use PUN (Photon Networking) though it displays it to all players (as all player's script detect the event) and all players can pickup the item. 当我使用PUN(光子网络)时,它将显示给所有玩家(因为所有玩家的脚本都检测到该事件),并且所有玩家都可以拾取该物品。 How I can fix this problem? 我该如何解决这个问题?

I believe fixing this piece will enable me to understand how to fix everything else: 我相信解决此问题将使我了解如何解决其他所有问题:

void OnTriggerEnter(Collider col)
{
    if (col.gameObject.tag == "Player")
    {
        displayMessage = true;
    }
}
void OnTriggerExit(Collider col)
{
    if (col.gameObject.tag == "Player")
    {
        displayMessage = false;
    }
}
bool stop = false;

void OnGUI()
{
    if (displayMessage)
    {

        if (GameObject.FindGameObjectWithTag("UInventory").GetComponent<UInventory>().inventoryMode == "Weight")
        {
            GUI.Label(new Rect((Screen.width/2)-200, (Screen.height/2)-30, 200, 45), "Press " + pickUpItem.ToString() + " To Take " + itemName + "(" + itemWeight + "kg)");
        }
        else
        {
            GUI.Label(new Rect((Screen.width/2)-200, (Screen.height/2)-30, 200, 45), "Press " + pickUpItem.ToString() + " To Take " + itemName);
        }
    }
}

Trigger code should be executed only if client owns network object. 仅当客户端拥有网络对象时,才应执行触发代码。 Use 'isMine' property of 'PhotonView' component of the object to check if this is true. 使用对象的“ PhotonView”组件的“ isMine”属性检查是否为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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