如何从非玩家对象 UNET 发送命令
How to send commands from non - player objects 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 ();
}
}
必须在与特定玩家关联的游戏对象(具有 NetworkBehavior)上调用命令。因此,我不相信可以从非玩家对象调用它们。
但是,您可以让枪支脚本与播放器上的脚本(与播放器连接相关联的 NetworkBehavior)进行通信。这将导致错误停止。 (这种通信可以通过 SendMessage 或只是对玩家对象的常规函数调用来完成。)
总而言之,目前,UNet 不允许来自非玩家对象的命令,因此需要对 player/gun 脚本的布局进行一些更改才能使用命令。但是,只需将枪支脚本 "talk to" 设为播放器脚本 -- 该脚本继承自与播放器连接相关联的 NetworkBehavior,只需几行代码即可完成此操作。
可以在此处查看有关命令的更多信息:
https://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html
我无法从游戏中的枪支预制件发送命令。如何从非玩家游戏对象发送命令?每次我尝试发送命令时,我都会收到错误消息。我不想将我的射击脚本从我的枪转移到我的玩家身上,因为这种方法需要我彻底改变我的游戏。
我的拍摄脚本:
[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 ();
}
}
必须在与特定玩家关联的游戏对象(具有 NetworkBehavior)上调用命令。因此,我不相信可以从非玩家对象调用它们。
但是,您可以让枪支脚本与播放器上的脚本(与播放器连接相关联的 NetworkBehavior)进行通信。这将导致错误停止。 (这种通信可以通过 SendMessage 或只是对玩家对象的常规函数调用来完成。)
总而言之,目前,UNet 不允许来自非玩家对象的命令,因此需要对 player/gun 脚本的布局进行一些更改才能使用命令。但是,只需将枪支脚本 "talk to" 设为播放器脚本 -- 该脚本继承自与播放器连接相关联的 NetworkBehavior,只需几行代码即可完成此操作。
可以在此处查看有关命令的更多信息: https://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html