我正在制作一个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;
}
}
注意:允许敌人放弃武器只有两个原因:currentAmmo等于0或者他遇到更好的武器(基于值)。
答案 0 :(得分:0)
关键字static表示此变量与脚本的所有实例共享。这就是导致你出现问题的原因。 - Catwood 1小时前 - 这是解决方案