C# 机器人以一种奇怪的方式拿起和放下武器
我正在制作一个TDS,所以现在是时候制作一个能够拿起和放下武器的机制了。我为玩家制作了一个新的机器人脚本,它基于玩家的脚本。所以问题是:我创建了一个敌人预制场,以便能够根据需要制造尽可能多的敌人,如果在游戏运行时只有一个敌人,那么一切都很好:敌人通过路标,拿起一件武器,然后遇到一件更好的武器,拿起一件更坏的武器。但是当我添加几个新的敌人时,事情变得很奇怪,因为敌人不会拿起武器(基本上,拿起的武器不会激活),而只是忽略它们。同时,地面上的武器在被拾取时通常会消失,但实际上却消失了,所以这没有任何意义。这是我实现的代码。我希望我们能找到解决这个问题的办法。 谢谢你的关注,也为我讨厌的英语感到抱歉C# 机器人以一种奇怪的方式拿起和放下武器,c#,unity3d,C#,Unity3d,我正在制作一个TDS,所以现在是时候制作一个能够拿起和放下武器的机制了。我为玩家制作了一个新的机器人脚本,它基于玩家的脚本。所以问题是:我创建了一个敌人预制场,以便能够根据需要制造尽可能多的敌人,如果在游戏运行时只有一个敌人,那么一切都很好:敌人通过路标,拿起一件武器,然后遇到一件更好的武器,拿起一件更坏的武器。但是当我添加几个新的敌人时,事情变得很奇怪,因为敌人不会拿起武器(基本上,拿起的武器不会激活),而只是忽略它们。同时,地面上的武器在被拾取时通常会消失,但实际上却消失了,所以这没有任何意
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class EnemyDrop : MonoBehaviour
{
//Current amount of ammo
public int currentAmmo;
private Vector3 offset;
//Weapons which are active in inventory.
//LABEL0
public GameObject _activeMelee;
public static GameObject activeMelee;
public GameObject _activeBottle;
public static GameObject activeBottle;
public GameObject _activeGun;
public static GameObject activeGun;
public GameObject _activePistol;
public static GameObject activePistol;
[HideInInspector]
//ID of weapon which was active last time.
public int activeId;
//ID of weapon which is ready to be picked up.
private int activeQuestion;
//Weapon that is equipped before function ChangeWeapon().
private GameObject currentChange;
//Weapon that is ready to be picked up in function ChangeWeapon().
private GameObject groundWeapon;
public Transform enemy;
[HideInInspector]
private bool ifThereIsSmthInHand;
[HideInInspector]
//Can we move to next step? This becomes true when enemy's trigger works.
public bool mightDoNextTurn;
[HideInInspector]
private string nameOfActive;
private string nameOfWeapon;
public GameObject[] arrayOfObjects;
public int[] arrayOfIds;
public int[] arrayOfAmmo;
[HideInInspector]
public bool isFlying;
[HideInInspector]
public float speed;
private float timeWhileFlying;
[HideInInspector]
public float timing;
public int[] arrayOfValues;
private bool pickedAnEmptyWeapon;
public string nameactive;
public string nameWeapon;
public int question;
public static string getActiveWeapon () //LABEL1
{
if (activeGun.activeSelf) {
return "Gun";
}
if (activeMelee.activeSelf) {
return "Melee";
}
if (activeBottle.activeSelf) {
return "Bottle";
}
if (activePistol.activeSelf) {
return "Pistol";
}
return "empty";
}
public static bool ifThereIsSomethingInHand ()//LABEL2
{
if (activeMelee.activeSelf || activeGun.activeSelf || activeBottle.activeSelf || activePistol.activeSelf)
return true;
else
return false;
}
void Start ()
{ //LABEL3
activeMelee = _activeMelee;
activeGun = _activeGun;
activeBottle = _activeBottle;
activePistol = _activePistol;
activeBottle.SetActive (false);
activeMelee.SetActive (false);
activeGun.SetActive (false);
activePistol.SetActive (false);
offset = new Vector3 (0f, 0.9f, -0.5f);
mightDoNextTurn = false;
speed = 2;
timeWhileFlying = 2;
activeId = 26;
}
void Update ()
{
ifThereIsSmthInHand = ifThereIsSomethingInHand ();
nameOfActive = getActiveWeapon ();
if (ifThereIsSmthInHand) {
if (mightDoNextTurn)
ChangeWeapon ();
else
actionDrop ();
} else if (mightDoNextTurn)
actionPick ();
nameactive = nameOfActive;
nameWeapon = nameOfWeapon;
question = activeQuestion;
}
private void actionDrop () //LABEL4
{
if (currentAmmo == 0 && nameOfActive == "Gun") {
activeGun.SetActive (false);
arrayOfObjects [activeId].SetActive (true);
arrayOfValues [activeId] = 0;
DropPos ();
activeId = 26;
}
if (currentAmmo == 0 && nameOfActive == "Pistol") {
activePistol.SetActive (false);
arrayOfObjects [activeId].SetActive (true);
arrayOfValues [activeId] = 0;
DropPos ();
activeId = 26;
}
}
public void deathDrop ()
{
arrayOfObjects [activeId].SetActive (true);
DropPos ();
}
private void actionPick ()//LABEL5
{
switch (nameOfWeapon) {
case "FireWeapon":
activeGun.SetActive (true);
break;
case "MeleeWeapon":
activeMelee.SetActive (true);
break;
case "BottleWeapon":
activeBottle.SetActive (true);
break;
case "PistolWeapon":
activePistol.SetActive (true);
break;
default:
break;
}
arrayOfObjects [activeQuestion].SetActive (false);
activeId = activeQuestion;
mightDoNextTurn = false;
}
private void DropPos ()
{
arrayOfObjects [activeId].transform.position = enemy.transform.position - offset;
arrayOfObjects [activeId].transform.rotation = enemy.transform.rotation;
}
private void ChangeWeapon ()//LABEL6
{
if (arrayOfValues [activeQuestion] > arrayOfValues [activeId]) {
if (activeMelee.activeSelf)
currentChange = activeMelee;
if (activeGun.activeSelf)
currentChange = activeGun;
if (activeBottle.activeSelf)
currentChange = activeBottle;
if (activePistol.activeSelf)
currentChange = activePistol;
currentChange.SetActive (false);
arrayOfObjects [activeId].SetActive (true);
DropPos ();
activeId = 26;
arrayOfObjects [activeQuestion].SetActive (false);
activeId = activeQuestion;
mightDoNextTurn = false;
if (nameOfWeapon == "BottleWeapon") {
activeBottle.SetActive (true);
}
if (nameOfWeapon == "MeleeWeapon") {
activeMelee.SetActive (true);
}
if (nameOfWeapon == "FireWeapon") {
activeGun.SetActive (true);
}
if (nameOfWeapon == "PistolWeapon") {
activePistol.SetActive (true);
}
}
}
void OnTriggerEnter (Collider other)//LABEL7
{
if (other.gameObject.tag == "Weapon") {
groundWeapon = other.gameObject;
activeQuestion = other.gameObject.GetComponent<IdOfWeapon> ().localId;
if (arrayOfValues [activeQuestion] == 0)
activeQuestion = 26;
if (activeQuestion >= 0 && activeQuestion <= 1000 && activeQuestion != 26) {
if (activeQuestion < 31) {
nameOfWeapon = "MeleeWeapon";
}
if (activeQuestion < 61 && activeQuestion >= 31) {
nameOfWeapon = "BottleWeapon";
}
if (activeQuestion >= 61 && activeQuestion < 91) {
nameOfWeapon = "FireWeapon";
}
if (activeQuestion >= 121 && activeQuestion < 151) {
nameOfWeapon = "PistolWeapon";
}
}
mightDoNextTurn = true;
}
}
void OnTriggerStay (Collider other)//LABEL8
{
if (other.gameObject.tag == "Weapon") {
groundWeapon = other.gameObject;
activeQuestion = other.gameObject.GetComponent<IdOfWeapon> ().localId;
if (arrayOfValues [activeQuestion] == 0)
activeQuestion = 26;
if (activeQuestion >= 0 && activeQuestion <= 1000 && activeQuestion != 26) {
if (activeQuestion < 31) {
nameOfWeapon = "MeleeWeapon";
}
if (activeQuestion < 61 && activeQuestion >= 31) {
nameOfWeapon = "BottleWeapon";
}
if (activeQuestion >= 61 && activeQuestion < 91) {
nameOfWeapon = "FireWeapon";
}
if (activeQuestion >= 121 && activeQuestion < 151) {
nameOfWeapon = "PistolWeapon";
}
}
mightDoNextTurn = true;
}
}
void OnTriggerExit (Collider other)
{
if (other.gameObject.tag == "Weapon") {
mightDoNextTurn = false;
nameOfWeapon = "";
}
activeQuestion = 26;
}
}
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEditor;
公共类EnemyDrop:单一行为
{
//当前弹药量
公共弹药;
专用矢量3偏移量;
//库存中活跃的武器。
//标签0
公共游戏对象(activeMelee);;
公共静态游戏对象动态近战;
公共游戏对象(activeball);;
公共静态游戏对象;
公共游戏对象(activeGun),;
公共静态游戏对象;
公共游戏对象(主动手枪);;
公共静态游戏对象主动手枪;
[hideininstecpt]
//上次激活的武器ID。
公共int-activeId;
//准备领取的武器ID。
私人问题;
//功能更改前装备的武器武器()。
私有游戏对象的变化;
//准备在函数changebarm()中拾取的武器。
私有游戏对象基础武器;
公敌;
[hideininstecpt]
私人楼宇(如有);
[hideininstecpt]
//我们能进入下一步吗?当敌人的扳机起作用时,这就变成了现实。
公共图书馆可能会转向;
[hideininstecpt]
活动的私有字符串名称;
武器的私有字符串名称;
公共游戏对象[]阵列对象;
公共int[]阵列FID;
公共机构【】arrayOfAmmo;
[hideininstecpt]
公共图书馆在飞行;
[hideininstecpt]
公众浮标速度;
飞行时的私人浮动时间;
[hideininstecpt]
公众浮动时间;
公共int[]数组值;
私人布尔pickedAnEmptyWeapon;
公共字符串名称活动;
公共武器;
公共问题;
公共静态字符串getActiveBearm()//LABEL1
{
if(activeGun.activeSelf){
返回“枪”;
}
if(activeMelee.activeSelf){
返回“近战”;
}
if(activeball.activeSelf){
归还“瓶子”;
}
if(activePistol.activeSelf){
返回“手枪”;
}
返回“空”;
}
公共静态bool if hereissomethinginhand()//LABEL2
{
如果(activeMelee.activeSelf | | | activeGun.activeSelf | | | | | activebaggle.activeSelf | | activePistol.activeSelf)
返回true;
其他的
返回false;
}
无效开始()
{//LABEL3
主动近战=_主动近战;
主动枪=_主动枪;
活动瓶=_活动瓶;
主动手枪=_主动手枪;
activeball.SetActive(false);
activeMelee.SetActive(false);
activeGun.SetActive(假);
activePistol.SetActive(假);
偏移量=新矢量3(0f,0.9f,-0.5f);
mightDoNextTurn=false;
速度=2;
飞行时间=2;
activeId=26;
}
无效更新()
{
Ifthereismthinnand=Ifthereismthinnand();
nameOfActive=getActiveBearm();
如果(如果此处为单侧){
如果(可能的话)
改变武器();
其他的
actionDrop();
}否则,如果(可能不返回)
actionPick();
nameactive=nameOfActive;
名称武器=武器名称;
问题=主动问题;
}
私有void actionDrop()//LABEL4
{
如果(currentAmmo==0&&nameOfActive==Gun){
activeGun.SetActive(假);
arrayOfObjects[activeId].SetActive(true);
arrayOfValues[activeId]=0;
DropPos();
activeId=26;
}
如果(currentAmmo==0&&nameOfActive==Pistol){
activePistol.SetActive(假);
arrayOfObjects[activeId].SetActive(true);
arrayOfValues[activeId]=0;
DropPos();
activeId=26;
}
}
公共空间死亡点()
{
arrayOfObjects[activeId].SetActive(true);
DropPos();
}
私有void actionPick()//LABEL5
{
开关(武器名称){
案例“Fireeapon”:
activeGun.SetActive(true);
打破
“MeleeWearm”案:
activeMelee.SetActive(true);
打破
“瓶装武器”案:
ActiveBattle.SetActive(真);
打破
“手枪武器”案:
activePistol.SetActive(真);
打破
违约:
打破
}
arrayOfObjects[activeQuestion].SetActive(false);
activeId=activeQuestion;
mightDoNextTurn=false;
}
私有void DropPos()
{
arrayOfObjects[activeId].transform.position=bi敌.transform.position-offset;
arrayOfObjects[activeId].transform.rotation=bi敌.transform.rotation;
}
私有void changefarm()//LABEL6
{
如果(arrayOfValues[activeQuestion]>arrayOfValues[activeId]){
if(activeMelee.activeSelf)
currentChange=主动近战;
if(activeGun.activeSelf)
currentChange=activeGun;
if(activeball.activeSelf)
currentChange=活动瓶;
if(activePistol.activeSelf)
电流变化=主动手枪;
currentChange.SetActive(假);
arrayOfObjects[activeId].SetActive(true);
DropPos();
activeId=26;
arrayOfObjects[activeQuestion].SetActive(false);
activeId=activeQuestion;
mightDoNextTurn=false;
如果(武器名称==“瓶装武器”){
ActiveBattle.SetActive(真);
}