简体   繁体   English

不是在Unity3d C#的所有客户端上执行ClientRpc函数

[英]ClientRpc Function not being carried out on all clients in Unity3d c#

Currently I am working on a networked 2d platformer game. 目前,我正在开发一个联网的2d平台游戏。 I have a script that is supposed to instantiate the players jetpack called JetpackManager. 我有一个脚本,该脚本应该实例化名为jetpackManager的玩家jetpack。 However when the player is spawned into the scene the code only spawns a jetpack into the hosts scene and not into the clients' scenes. 但是,当将玩家派生到场景中时,代码仅将Jetpack派生到主机场景中,而不是客户场景中。 This results in only the hosts scene working properly while all the players in the clients' scenes have no jetpacks. 这导致只有主持人场景可以正常工作,而客户场景中的所有玩家都没有背包。 This is my code for the JetpackManager: 这是我的JetpackManager代码:

using UnityEngine;
using UnityEngine.Networking;

public class JetpackManager : NetworkBehaviour {

[SerializeField]
private Jetpack[] jetpacks;

[SerializeField]
private Transform jetPackHold;

private PlayerController playerController;

private Jetpack currentJetpack;

void Start(){
    playerController = GetComponent<PlayerController> ();

    if (isLocalPlayer) {
        CmdEquipJetpack (0);
    }
}

[Command]
void CmdEquipJetpack(int jetpackNumber){
    RpcEquipJetpack (jetpackNumber);
}

[ClientRpc]
void RpcEquipJetpack(int jetpackNumber){

    if (currentJetpack != null) {
        Destroy (currentJetpack.gameObject);
    }

    currentJetpack = Instantiate (jetpacks[jetpackNumber], jetPackHold.position, jetPackHold.rotation);
    currentJetpack.transform.SetParent (jetPackHold);

    playerController.InitialiseJetpackVariables (currentJetpack);
}

} }

So essentially my problem is that the code within the RpcEquipJetpack Function is for some reason only being called on the host and not on any of the clients. 所以本质上我的问题是,出于某种原因, RpcEquipJetpack函数中的代码仅在主机上调用,而不在任何客户端上调用。

You should instantiate Network object with network class 您应该使用网络类实例化网络对象

Network.Instantiate 网络实例化

Network instantiate a prefab. 网络实例化一个预制件。

The given prefab will be instanted on all clients in the game. 给定的预制件将在游戏中的所有客户端上实例化。 Synchronization is automatically set up so there is no extra work involved. 同步是自动设置的,因此不涉及额外的工作。 The position, rotation and network group number are given as parameters. 位置,旋转和网络组号作为参数给出。 Note that in the example below there must be something set to the playerPrefab in the Editor. 请注意,在下面的示例中,必须在编辑器中将playerPrefab设置为某些内容。 You can read more about instantiations in the object reference Object.Instantiate. 您可以在对象参考Object.Instantiate中阅读有关实例化的更多信息。

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

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