如何从非玩家对象发送命令UNET

时间:2016-06-26 17:09:27

标签: c# networking unity3d game-engine unity3d-unet

我无法在游戏中从我的枪预制件发送命令。如何从非玩家游戏对象发送命令?每次我尝试发送命令时,都会出错。我不想将我的射击脚本从我的枪支移动到我的播放器,因为这种方法需要我彻底改变我的游戏。

我的拍摄剧本:

[Command]
    void CmdOpenFire(){


    if (Physics.Raycast(camTransform.TransformPoint(startPosition), camTransform.forward, out hit, range)){
        gunsMasterScript.CallEventShotDefault(hit.point, hit.transform);
        if (hit.transform.parent != null) {
            if (hit.transform.parent.CompareTag ("Enemy")) {

                gunsMasterScript.CallEventShotEnemy (hit.point, hit.transform.parent);
            }
        }


        if (hit.transform.tag == "Player") {
            gunsMasterScript.CallEventShotEnemy (hit.point, hit.transform);
        }
    }

}

我的拍摄检测脚本:

public PlayerManager_AmmoBox localPlayerTester;
public GunsManager_GunsAmmo gunsAmmo;
public GunsManager_GunsMaster gunMaster;
float nextAttack;
public float attackRate = 0.5f; 
Transform myTransform;
public bool isAutomatic = true;
public bool hasBurstFire;
bool isBurstFireActive;
public string attackButtonName;
public string reloadButtonName;
public string burstFireButtonName;
NetworkIdentity netID;

// Use this for initialization
void OnEnable () {
    if (transform.root != null) {
        if (transform.root.GetComponent<PlayerManager_AmmoBox> () != null) {
            localPlayerTester = transform.root.GetComponent<PlayerManager_AmmoBox> ();
        }
    }
    SetInitialReferences ();

}

// Update is called once per frame
void Update () {
    if (localPlayerTester != null) {
        CheckIfWeaponShouldAttack ();
        CheckForReloadRequest ();
        CheckForBurstFireToggle ();
    }

}
void SetInitialReferences(){
    netID = GetComponent<NetworkIdentity> ();
    myTransform = transform;
    gunMaster = transform.GetComponent<GunsManager_GunsMaster> ();
    gunsAmmo = transform.GetComponent<GunsManager_GunsAmmo> ();
     gunMaster.isGunLoaded = true; 
}


void CheckIfWeaponShouldAttack(){

    if (gunMaster != null) {
        if (Time.time > nextAttack && Time.timeScale > 0 && myTransform.root.CompareTag ("Player") && gunMaster.isGunLoaded) {


                if (isAutomatic && !isBurstFireActive) {
                if (Input.GetButton (attackButtonName)) {
                    AttemptAttack ();
                }

            } else if (isAutomatic && isBurstFireActive) {
                if (Input.GetButtonDown (attackButtonName)) {
                    StartCoroutine (RunBurstFire ());

                }

            } else if (!isAutomatic) {

                if (Input.GetButton (attackButtonName)) {
                    AttemptAttack ();
                }
            }

        }
    }
}

void AttemptAttack(){
    nextAttack = Time.time + attackRate;
    if (gunMaster.isGunLoaded) {

        gunMaster.CallEventPlayerInput ();

    } else {

        gunMaster.CallEventGunNotUsable ();
    }
}

1 个答案:

答案 0 :(得分:1)

必须在与某个玩家相关联的gameobject(具有NetworkBehavior)上调用命令。因此,我不相信他们可以从非玩家对象中调用。

但是,您可以让枪脚本与播放器上的脚本(与播放器连接关联的NetworkBehavior)进行通信。这会导致错误停止。 (这种通信可以通过SendMessage完成,也可以只对播放器对象进行常规函数调用。)

总之,目前,UNet不允许来自非玩家对象的命令,因此需要对播放器/枪支脚本的布局进行一些更改才能使用命令。但是,只需几行代码就可以完成,只需使枪脚脚本与玩家脚本“对话” - 从与玩家连接相关联的NetworkBehavior继承的脚本。

有关命令的更多信息,请参见此处: https://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html