家人们,我在试图仿一个合成大西瓜的小游戏,然后遇到一个bug,就是当一个球同时碰到两个和它一样的球时,比如A同时碰到了B和C,那么三个球都会消失并生成两个更大的球
以下是我挂载到每一个小球上的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : BallAttribute
{
public BallType type = BallType.zero;
public BallScore score = BallScore.zero;
private static bool isCombining = false;
private void Start()
{
ScoreText.Instance.score += (int)score;
ScoreText.Instance.RefreshScore();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (type == BallType.nine) return;
if (isCombining) return;
foreach (ContactPoint2D contact in collision.contacts)
{
// 获取碰撞点上的物体
GameObject obj = contact.collider.gameObject;
if (obj.CompareTag("Ball")) {
Ball otherBall = obj.GetComponent<Ball>();
if (otherBall.type == type) {
if (transform.position.y < obj.transform.position.y) {
int next = (int)type + 1;
if (next < 10) {
BallAudio.Instance.Ber((int)type);
Instantiate(BallsBasket.Instance.balls[(int)type + 1], MidPosition(transform.position, obj.transform.position), transform.rotation);
isCombining = true;
}
}
if (obj != null) {
Destroy(obj);
}
Destroy(gameObject);
}
}
}
}
}
想问问各位大佬有没有什么解决方法,求浇浇