【问题标题】:How to send commands from non - player objects UNET如何从非玩家对象 UNET 发送命令
【发布时间】:2016-06-26 17:09:26
【问题描述】:

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

我的拍摄脚本:

[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 ();
    }
}

【问题讨论】:

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


    【解决方案1】:

    必须在与特定玩家关联的游戏对象(具有 NetworkBehavior)上调用命令。因此,我不相信它们可以从非玩家对象中调用。

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

    总之,目前,UNet 不允许来自非玩家对象的命令,因此需要对玩家/枪支脚本的布局进行一些更改才能使用命令。然而,这可以通过让枪脚本与玩家脚本“对话”来仅用几行代码来完成——该脚本继承自与玩家连接相关联的 NetworkBehavior。

    关于命令的更多信息可以在这里看到: https://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html

    【讨论】:

    • 我尝试在我的脚本中使用你的方法,但我收到了另一个错误。你介意看看我的最新问题吗? @user3210251
    猜你喜欢
    • 2017-04-28
    • 2016-12-21
    • 2017-02-13
    • 2016-02-01
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多