武器仅对服务器主机可见

Weapon is visible only to server host

好吧,我做了一个简单的多人射击游戏,到目前为止一切正常,除了选择的武器没有显示给任何人,但主机例如如果我选择武器 1 (ak47) 除了服务器之外没有人能看到对象主机 我的其他武器也一样,我相信我需要用 NetworkServer.Spawn 生成它们,但我不知道该怎么做。感谢您的回答!!!

using UnityEngine;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class PlayerController : NetworkBehaviour
{
public Text Ammoleft;
[SyncVar]
public int Ak47Bullets = 30;

[SyncVar]
public int PipeShotgunBullets = 7;

[SyncVar]
public int currentWeapon;

public Transform[] weapons;

[SyncVar]
public string CurrentWeaponstr;

public GameObject ShottyBullet;
public GameObject Ak47Bullet;
public Transform bulletSpawn;
public float speed = 1; // speed in meters per second


void Update()
{

    if (!isLocalPlayer)
    {
        return;
    }

    float mouseInput = Input.GetAxis("Mouse X");
    Vector3 lookhere = new Vector3(0,mouseInput,0);
    transform.Rotate(lookhere);

    var x = Input.GetAxis("Horizontal") * Time.deltaTime * 3.0f;
    var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;

    transform.Translate(x, 0, 0);
    transform.Translate(0, 0, z);

    Vector3 moveDir = Vector3.zero;
    //moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
    //moveDir.z = Input.GetAxis("Vertical"); // get result of WS keys in Z
    // move this object at frame rate independent speed:
    transform.position += moveDir * speed * Time.deltaTime;

    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        CmdSwitchtoAk();
    }
    if (Input.GetKeyDown(KeyCode.Alpha2))
    {
        CmdSwitchtoPipe();
    }

    if (Input.GetKeyDown (KeyCode.Mouse0) && CurrentWeaponstr == 
    "PipeShotgun" && PipeShotgunBullets > 0)
    {
        CmdFireShotty();
        PipeShotgunBullets -= 1;
        Ammoleft.text = "Left: " + PipeShotgunBullets;
        Debug.Log ("Shotpipe");
    }
    if (Input.GetKey (KeyCode.Mouse0) && CurrentWeaponstr == "Ak47" && 
    Ak47Bullets > 0) {
        CmdFireAk47();
        Ak47Bullets -= 1;
        Ammoleft.text = "Left: " + Ak47Bullets;
        Debug.Log ("ShotAk");
    }
}

[Command]
void CmdSwitchtoAk()
{
        changeWeapon(1);
        CurrentWeaponstr = "Ak47";

}

[Command]
void CmdSwitchtoPipe()
{
    changeWeapon(2);
    CurrentWeaponstr = "PipeShotgun";
}

// This [Command] code is called on the Client …
// … but it is run on the Server!
[Command]
void CmdFireAk47(){
    if (CurrentWeaponstr == "Ak47") {
        var Ak47bullet = (GameObject)Instantiate (
            Ak47Bullet,
            bulletSpawn.position,
            bulletSpawn.rotation);
        Ak47bullet.GetComponent<Rigidbody>().velocity = Ak47bullet.transform.forward * 6;
        NetworkServer.Spawn(Ak47bullet);
    }
}
[Command]
void CmdFireShotty()
{

    // Create the Bullet from the Bullet Prefab
    if (CurrentWeaponstr == "PipeShotgun") {
        var Pipebullet = (GameObject)Instantiate (
                         ShottyBullet,
                         bulletSpawn.position,
                         bulletSpawn.rotation);
        Pipebullet.GetComponent<Rigidbody>().velocity = Pipebullet.transform.forward * 6;
        NetworkServer.Spawn(Pipebullet);

    }

}

public override void OnStartLocalPlayer ()
{
    GetComponent<MeshRenderer>().material.color = Color.blue;
}

public void changeWeapon(int num) {

    currentWeapon = num;
    for(int i = 0; i < weapons.Length; i++) {
        if (i == num) {
            weapons [i].gameObject.SetActive (true);
        }
        else
            weapons[i].gameObject.SetActive(false);         
    }
    }
    }

这是图片:

您正在调用一个命令,这就是为什么它在 server/host 上只有 运行,命令是服务器代码,而 RPC 是客户端代码,所以如果您想将一些操作发送到每个人都需要从 Server/Host.

调用 RPC
public class PlayerController : NetworkBehaviour
{
    [Command]
    void CmdSwitchtoAk()
    {
        RpcSwitchtoAk();
    }

    [ClientRpc]
    void RpcSwitchtoAk()
    {
        changeWeapon(1);
        CurrentWeaponstr = "Ak47";
    }
}
  1. 客户端调用命令
  2. 服务器上的命令运行,然后使用RPC转发给所有客户端
  3. 服务器调用 RPC
  4. 所有客户端得到回调

参考:Unity Unet Manual