预制件无法为其他玩家同步

时间:2019-04-09 05:28:12

标签: c# unity3d

我有带有 Network Transform Network Identity 的Bullet预制件。

  

生成子弹的播放器脚本:

using UnityEngine;

public class Weapon : MonoBehaviour {

    bool ready = true;
    public Transform firePoint;
    public bool shot;
    public GameObject bulletPrefab;

    void FixedUpdate () {
        if ((Input.GetKey(KeyCode.Space)) || shot)
        {
            if (ready)
            {
                Shoot();
            }
            else
            {
                // click sound
            }
        }
    }

    void Shoot()
    {
        ready = false;
        var bulletGo = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
        LeanTween.delayedCall(6f, () => { ready = true; });
    }
}

但是当我射击时,我的敌人看不到这枚子弹。 为什么不同步?我的播放器还具有 Network Transform Network Identity ,一切正常。

1 个答案:

答案 0 :(得分:1)

要生成网络对象,Unity提供了特定的功能NetworkServer.Spawnhttps://docs.unity3d.com/ScriptReference/Networking.NetworkServer.Spawn.html

实例化对象后,需要调用此函数,以便所有客户端在其场景中生成它。

对于您的情况,在实例化bulletGo对象之后,应调用:

NetworkServer.Spawn(bulletGo)