简体   繁体   English

Unity,C#,网络,3D。 实例化预制件(武器)不会在所有客户端上显示

[英]Unity, C#, Network, 3D. Instantiating Prefab(Weapon) wont show on all Clients

I have 3 Gun Prefabs in an FPS Multiplayer. 我在FPS多人游戏中有3个Gun Prefabs。 When I spawn different prefabs I only see it on the local client. 当我生成不同的预制件时,我只能在本地客户端上看到它。 Other clients still only see the Base Weapon GFX. 其他客户仍然只能看到基本武器GFX。 Is there a simple way for all players to see the weapon graphics which I Instantiate on the local client? 有没有一种简单的方法可以让所有玩家看到我在本地客户端上实例化的武器图形?

Animations, movement, rotations seem to work perfectly. 动画,运动,旋转似乎完美。 Only the currentGFX of the local weapon wont change on other clients 只有本地武器的当前GFX不会在其他客户端上更改

Here is my code for the Instantiate weapon part: 这是我的“实例化”武器部分的代码:

        public void SpawnWeapon(PlayerWeapon _weapon)
{
    currentWeapon = _weapon;

    //Instantiate the prefab
    GameObject _weaponIns = (GameObject)Instantiate(_weapon.graphics, weaponHolder, false);
//Assign  the weaponGFX BUT works only on the local client
    currentGraphics = _weaponIns.GetComponent<WeaponGraphics>();
}

You need to spawn weapon on the server. 您需要在服务器上生成武器。

you need add: 您需要添加:

using UnityEngine.Networking;

in your script, then type 在脚本中,然后键入

[Command] above your method and spawn object on the server like: 方法上方的[Command]和服务器上的生成对象,如下所示:

[Command] 
public void SpawnWeapon(PlayerWeapon _weapon)
{

currentWeapon = _weapon;
GameObject _weaponIns = (GameObject)Instantiate(_weapon.graphics, weaponHolder, 
false);
currentGraphics = _weaponIns.GetComponent<WeaponGraphics>();

NetworkServer.SpawnWithClientAuthority(_weaponIns, 
PLAYERS_NETWORK_IDENTITY.connectionToClient);
}

It will cause server to update it's data and then other clients will be updated. 这将导致服务器更新其数据,然后其他客户端将被更新。

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

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