实例化在触发器上不断重复自己,该触发器在冲突时删除自己



我一直在尝试用 C# 制作一个无限的颜色切换游戏副本。 我的计划是在旋转圆圈内有一个绿色点。与玩家碰撞时,这将给用户一个分数并删除绿色圆圈。以及激活触发器以生成一个新圆圈,该圆的 y 值比消耗的点的当前位置高 14。开始总是已经有 2 个圆圈,因此当您向上移动时,您不会看到新圆圈形成,因为它在前面的第二个圆圈中生成。 有一个销毁块,可以向下删除玩家视野之外的所有块,以减少旧圆圈建立时的滞后,或者在玩家死亡时重新启动游戏。 问题是在同一个位置创建了多个圆圈,正如 CurrentSpawnPoint 的调试所看到的那样,对于多个生成或高度似乎迅速增加,就好像它正在繁殖或只是快速添加一样,这些圆圈保持不变。 我只是 Unity 的初学者,仍然不知道所有的 C# 变量,当然有一个简单的解决方案来解决我的问题。任何帮助,非常感谢,感谢您的时间。 如果我的描述仍然不清楚,这里是指向实际游戏文件的 GitHub 链接。https://github.com/Xotsu/Color-Switch-replica

using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour {
public float jumpForce = 7f; 
public Rigidbody2D rb;
public SpriteRenderer sr;
public string currentColor;
public GameObject Green;
public GameObject[] obj;
public float SpawnAmount = 1f;
public Color ColorBlue;
public Color ColorPurple;
public Color ColorYellow;
public Color ColorPink;
public int Score = 0;
public int Height = 0;
Vector3 CurrentSpawnPoint;
private void Start()
{
SetRandomColor();
}
void Update() 
{
if (Input.GetButtonDown("Jump") || Input.GetMouseButtonDown(0))
{
rb.velocity = Vector2.up * jumpForce;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Change")
{
SetRandomColor();
Destroy(col.gameObject);
return;
}
else if (col.tag == "Point")
{
Debug.Log("Point gained");
Destroy(col.gameObject);
Score += 1;
Height += 7;
CurrentSpawnPoint = col.transform.position;
CurrentSpawnPoint.y += 7;
Instantiate(Green, CurrentSpawnPoint, Quaternion.identity);
Instantiate(obj[Random.Range(0, obj.GetLength(0))], CurrentSpawnPoint,  Quaternion.identity);
Debug.Log(Height);
Debug.Log(CurrentSpawnPoint);
return;
}
else if (col.tag != currentColor)
{
Debug.Log("GAME OVER");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
void SetRandomColor()
{
int index = Random.Range(0, 4);
switch (index)
{
case 0:
currentColor = "Blue";
sr.color = ColorBlue;
break;
case 1:
currentColor = "Yellow";
sr.color = ColorYellow;
break;
case 2:
currentColor = "Purple";
sr.color = ColorPurple;
break;
case 3:
currentColor = "Pink";
sr.color = ColorPink;
break;
}
}
}  

问题是我在绿点中独立生成,同时在圆圈预制件中生成重复项。对不起,愚蠢的问题:/

最新更新