Instantiate 在触发时不断重复自身,触发时会自行删除
Instantiate keeps repeating itself on trigger which deletes itself upon collision
我一直在尝试用 C# 制作一个无限颜色切换游戏副本。
我的计划是在旋转的圆圈内有一个绿点。在与玩家发生碰撞时,这会给用户一个分数并删除绿色圆圈。以及激活触发器以生成一个新圆圈,该圆圈的 y 值比消耗点的当前位置高 14。一开始总会有 2 个圆圈,因此当你向上移动时,你看不到新的圆圈形成,因为它在前面的第二个圆圈中产生。
有一个 destroy 方块可以向下删除玩家视野之外的所有方块,以减少旧圆圈堆积时的延迟,或者在玩家死亡时重新开始游戏。
问题是在 CurrentSpawnPoint 的调试中看到的相同位置创建了多个圆圈,它对于多个生成保持不变,或者高度似乎在快速增加,就像它正在快速增加或只是快速增加一样。
我只是 unity 的初学者,仍然不知道所有的 C# 变量,当然我的问题有一个简单的解决方案。感谢您的帮助,感谢您的宝贵时间。
如果我的描述仍然不清楚,这里是实际游戏文件的 GitHub link。 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;
}
}
}
问题是我在绿点中独立生成,同时在圆形预制件中生成重复项。抱歉这个愚蠢的问题:/
我一直在尝试用 C# 制作一个无限颜色切换游戏副本。 我的计划是在旋转的圆圈内有一个绿点。在与玩家发生碰撞时,这会给用户一个分数并删除绿色圆圈。以及激活触发器以生成一个新圆圈,该圆圈的 y 值比消耗点的当前位置高 14。一开始总会有 2 个圆圈,因此当你向上移动时,你看不到新的圆圈形成,因为它在前面的第二个圆圈中产生。 有一个 destroy 方块可以向下删除玩家视野之外的所有方块,以减少旧圆圈堆积时的延迟,或者在玩家死亡时重新开始游戏。 问题是在 CurrentSpawnPoint 的调试中看到的相同位置创建了多个圆圈,它对于多个生成保持不变,或者高度似乎在快速增加,就像它正在快速增加或只是快速增加一样。 我只是 unity 的初学者,仍然不知道所有的 C# 变量,当然我的问题有一个简单的解决方案。感谢您的帮助,感谢您的宝贵时间。 如果我的描述仍然不清楚,这里是实际游戏文件的 GitHub link。 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;
}
}
}
问题是我在绿点中独立生成,同时在圆形预制件中生成重复项。抱歉这个愚蠢的问题:/