TO SQUASH: better ball collisions.

This commit is contained in:
2026-08-03 01:47:14 -04:00
parent 378efa16ef
commit 17051821b5
8 changed files with 50 additions and 10 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ GameObject:
- component: {fileID: 3177825165818360414} - component: {fileID: 3177825165818360414}
m_Layer: 0 m_Layer: 0
m_Name: Basic Ball m_Name: Basic Ball
m_TagString: Ball m_TagString: BasicBall
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
+1 -1
View File
@@ -17,7 +17,7 @@ GameObject:
- component: {fileID: 36043693063624440} - component: {fileID: 36043693063624440}
m_Layer: 0 m_Layer: 0
m_Name: Fruit Ball m_Name: Fruit Ball
m_TagString: Ball m_TagString: FruitBall
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
+1 -1
View File
@@ -17,7 +17,7 @@ GameObject:
- component: {fileID: 2721640963658894789} - component: {fileID: 2721640963658894789}
m_Layer: 0 m_Layer: 0
m_Name: Ooze Ball m_Name: Ooze Ball
m_TagString: Untagged m_TagString: OozeBall
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
+35 -3
View File
@@ -22,6 +22,8 @@ public class BallSpawner : MonoBehaviour
private Queue<System.Action> ballQueue = new Queue<System.Action>(); private Queue<System.Action> ballQueue = new Queue<System.Action>();
private Queue<GameObject> balls = new Queue<GameObject>();
// Start is called once before the first execution of Update after the MonoBehaviour is created // Start is called once before the first execution of Update after the MonoBehaviour is created
void Start() void Start()
{ {
@@ -46,7 +48,9 @@ public class BallSpawner : MonoBehaviour
} }
Vector3 spawnPosition = GetRandomPositionInsideGameBoard(); Vector3 spawnPosition = GetRandomPositionInsideGameBoard();
Instantiate(ballPrefab, spawnPosition, Quaternion.identity); var newBall = Instantiate(ballPrefab, spawnPosition, Quaternion.identity);
balls.Enqueue(newBall);
} }
private Vector3 GetRandomPositionInsideGameBoard() private Vector3 GetRandomPositionInsideGameBoard()
@@ -70,8 +74,9 @@ public class BallSpawner : MonoBehaviour
bounds = boardRenderer.bounds; bounds = boardRenderer.bounds;
} }
float randomX = Random.Range(bounds.min.x, bounds.max.x); const float edgeInset = 0.05f;
float randomY = Random.Range(bounds.min.y, bounds.max.y); float randomX = Random.Range(bounds.min.x + edgeInset, bounds.max.x - edgeInset);
float randomY = Random.Range(bounds.min.y + edgeInset, bounds.max.y - edgeInset);
return new Vector3(randomX, randomY, gameBoard.transform.position.z); return new Vector3(randomX, randomY, gameBoard.transform.position.z);
} }
@@ -84,5 +89,32 @@ public class BallSpawner : MonoBehaviour
spawnAction.Invoke(); spawnAction.Invoke();
yield return _waitForSeconds0_5; yield return _waitForSeconds0_5;
} }
// Disable collision between existing balls of certain types.
// Only basic balls and fruit balls will collide with each other, while ooze,
// nuclear, glass, and eye balls will not collide with any other balls.
// As an exception, glass balls will collide with other glass balls.
foreach (var ball in balls)
{
foreach (var otherBall in balls)
{
if (ball == otherBall) continue;
bool ballIsBasicOrFruit = ball.CompareTag("BasicBall") || ball.CompareTag("FruitBall");
bool otherIsBasicOrFruit = otherBall.CompareTag("BasicBall") || otherBall.CompareTag("FruitBall");
bool bothAreGlass = ball.CompareTag("GlassBall") && otherBall.CompareTag("GlassBall");
bool shouldCollide = (ballIsBasicOrFruit && otherIsBasicOrFruit) || bothAreGlass;
if (!shouldCollide)
{
Physics2D.IgnoreCollision(
ball.GetComponent<Collider2D>(),
otherBall.GetComponent<Collider2D>(),
true
);
}
}
}
} }
} }
+3 -1
View File
@@ -36,7 +36,9 @@ public class BasicBall : MonoBehaviour
void OnCollisionEnter2D(Collision2D collision) void OnCollisionEnter2D(Collision2D collision)
{ {
if (collision.gameObject.CompareTag("Wall") || collision.gameObject.CompareTag("Ball")) if (collision.gameObject.CompareTag("Wall") ||
collision.gameObject.CompareTag("BasicBall") ||
collision.gameObject.CompareTag("FruitBall"))
{ {
ContactPoint2D contact = collision.GetContact(0); ContactPoint2D contact = collision.GetContact(0);
speed = Vector2.Reflect(speed, contact.normal); speed = Vector2.Reflect(speed, contact.normal);
+5 -2
View File
@@ -50,12 +50,15 @@ public class FruitBall : MonoBehaviour
void OnCollisionEnter2D(Collision2D collision) void OnCollisionEnter2D(Collision2D collision)
{ {
if (collision.gameObject.CompareTag("Wall") || collision.gameObject.CompareTag("Ball")) if (collision.gameObject.CompareTag("Wall") ||
collision.gameObject.CompareTag("BasicBall") ||
collision.gameObject.CompareTag("FruitBall"))
{ {
ContactPoint2D contact = collision.GetContact(0); ContactPoint2D contact = collision.GetContact(0);
speed = Vector2.Reflect(speed, contact.normal); speed = Vector2.Reflect(speed, contact.normal);
if (collision.gameObject.CompareTag("Ball")) if (collision.gameObject.CompareTag("BasicBall") ||
collision.gameObject.CompareTag("FruitBall"))
{ {
speedIncreaseTimer = 2.0f; // Reset the timer to 2 seconds speedIncreaseTimer = 2.0f; // Reset the timer to 2 seconds
speedMultiplier = 50.0f; // Set the speed multiplier to 7x speedMultiplier = 50.0f; // Set the speed multiplier to 7x
+1 -1
View File
@@ -29,7 +29,7 @@ public class OozeBall : MonoBehaviour
{ {
audioSource = GetComponent<AudioSource>(); audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>(); animator = GetComponent<Animator>();
speed = new Vector2(Random.Range(-0.005f, 0.005f), Random.Range(-0.005f, 0.005f)); speed = new Vector2(Random.Range(-0.002f, 0.002f), Random.Range(-0.002f, 0.002f));
audioSource.PlayOneShot(spawnSound); audioSource.PlayOneShot(spawnSound);
} }
+3
View File
@@ -6,6 +6,9 @@ TagManager:
tags: tags:
- Wall - Wall
- Ball - Ball
- BasicBall
- FruitBall
- OozeBall
layers: layers:
- Default - Default
- TransparentFX - TransparentFX