我正在尝试制作一个像轰炸机一样的游戏,同时学习 c#。
我尝试在我的代码中更改 targetDestroyed1 var 的布尔值,但似乎有问题..
问题:在GetNearestTaggedObject1 中,当一个对象被创建时,targetDestroyed1 变为真。但是在 InstantiateFire() 的 if (!targetDestroyed1) 条件下,targetDestroyed1 始终保持为 false 并且实例化继续而不是停止
这是我的代码:
private bool targetDestroyed1;
void Start (){
OriginXplosion = new Vector3(transform.position.x, transform.position.y, transform.position.z);
InstantiateFire();
}
void Update (){
if (target1 != null) {
Destroy (target1);
}
public void InstantiateFire (){
FireRate = player_actions.FireRate;
FireRate -= 1;
targetDestroyed1 = false;
for(int i1= 0; i1 <= FireRate; i1++){
float i1_axeZ= transform.position.z + i1;
if(i1_axeZ <= 7)
{
if (!targetDestroyed1)
{
Instantiate (ParticulesFeu, Axe1[i1].transform.position, ParticulesFeu.transform.rotation);
ScanForTarget1();
}
}
}
Destroy (gameObject);
}
void ScanForTarget1 (){
target1 = GetNearestTaggedObject1();
}
GameObject GetNearestTaggedObject1 (){
float nearestDistanceSqr= 0.2f;
GameObject[] taggedGameObjects= GameObject.FindGameObjectsWithTag(searchTag);
foreach(GameObject obj in taggedGameObjects) {
Vector3 objectPos= obj.transform.position;
float distanceSqr= (objectPos - transform.position).sqrMagnitude;
if (distanceSqr < nearestDistanceSqr) {
target1 = obj;
targetDestroyed1 = true;
nearestDistanceSqr = distanceSqr;
}
}
return target1;
}
}
谢谢您的帮助 !