1

I am making shooting game over network and it works very well with below source.

player.cs

void Update(){
    if (!isLocalPlayer) {
        return;
    }
    if (Input.GetMouseButton(0)) {
        CmdDefaultAttack(_skillDefault);
    }

}

[Command]
protected void CmdDefaultAttack(GameObject _attackObject) {

    GameObject bullet = (GameObject)Instantiate(_attackObject,_skill_Default_Spawn.position, _skill_Default_Spawn.rotation);

    bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6.0f;

    ClientScene.RegisterPrefab(bullet);

    NetworkServer.Spawn(bullet);

    Destroy(bullet, 2);
}


but what i want to do is to implement CmdDefaultAttack in another class inherited Interface and call it from Player.cs

source below

public interface ISkill {
    void initiate(Player _player);
    void CmdAttack();
    bool CmdMotion();
}

public class SphereSkillDefault : NetworkBehaviour, ISkill {
    Plyaer player;
    public GameObject _skill_Default_Level1;
    public GameObject _skill_Default_Cannon

    public void initiate(Player_player) {
        this.player = _player;
    }
    [Command]
    public void CmdAttack() {
        GameObject bullet = (GameObject)Instantiate(_skill_Default_Level1, player._skill_Default_Spawn.position, player._skill_Default_Spawn.rotation);
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6.0f;
        NetworkServer.Spawn(bullet);
        Destroy(bullet, 2);
    }

}

player.cs

    ISkill _Idefaultattack;
    public GameObject _defaultattack;

void Start() {
        if (NetworkServer.active)
            Debug.Log("Actived");
        else
            Debug.Log("DeActived");

        if (isLocalPlayer) {
            _Idefaultattack = _defaultattack.GetComponent<SphereSkillDefault>();
            _Idefaultattack.initiate(this);
        }
    }
    void Update() {
        if (!isLocalPlayer) {
            return;
        }
        _Idefaultattack.CmdAttack();
    }

The problem is when i try to shoot a bullet, it throws the error message saying "network server is not active. cannot spawn objects without an active server."

And only server player can shoot bullets and it actually shows up to clients. But clients can shoot only on their side and it doesn't show up to other clients or server.

i am stuck in this problem for days. Can anyone give me any advice?

thanks.

4

1 回答 1

2

这个问题是直接调用其他类。所以,你需要桥接功能。我已经写了源码。

ISkill _Idefaultattack;
public GameObject _defaultattack;

void Start() {
    if (NetworkServer.active)
        Debug.Log("Actived");
    else
        Debug.Log("DeActived");

    if (isLocalPlayer) {
        _Idefaultattack = _defaultattack.GetComponent<SphereSkillDefault>();
        _Idefaultattack.initiate(this);
    }
}
void Update() {
    if (!isLocalPlayer) {
        return;
    }
    //_Idefaultattack.CmdAttack();
    CmdAck();
}

[Command]
void CmdAck(){
    _Idefaultattack.CmdAttack();
}
于 2016-07-28T11:31:41.277 回答