TO SQUASH: Fixed collision issues with the shark.

This commit is contained in:
2026-08-11 22:35:28 -04:00
parent 69beb2496d
commit 59e6e73b20
6 changed files with 171 additions and 18 deletions
+32 -4
View File
@@ -11,6 +11,8 @@ public class Shark : MonoBehaviour
public AudioClip deathSound;
public AudioClip hitSound;
public Player player;
public SpriteRenderer gameBoardSpriteRenderer;
@@ -113,10 +115,10 @@ public class Shark : MonoBehaviour
Bounds sharkBounds = sharkSpriteRenderer.bounds;
Bounds boardBounds = gameBoardBounds.Value;
float minX = boardBounds.min.x + sharkBounds.size.x;
float maxX = boardBounds.max.x - sharkBounds.size.x;
float minY = boardBounds.min.y + sharkBounds.size.y;
float maxY = boardBounds.max.y - sharkBounds.size.y;
float minX = boardBounds.min.x + (2.0f * sharkBounds.size.x);
float maxX = boardBounds.max.x - (2.0f * sharkBounds.size.x);
float minY = boardBounds.min.y + (2.0f * sharkBounds.size.y);
float maxY = boardBounds.max.y - (2.0f * sharkBounds.size.y);
// Fallback to board center if the shrunken region collapses.
if (minX > maxX)
@@ -286,4 +288,30 @@ public class Shark : MonoBehaviour
);
Destroy(gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Bar"))
{
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(hitSound);
}
// Destroy all bar segments when the ball hits a bar.
var barSegments = GameObject.FindGameObjectsWithTag("Bar");
foreach (var barSegment in barSegments)
{
Destroy(barSegment);
}
// Notify the player that the bar has been hit.
var player = GameObject.FindGameObjectWithTag("Player");
if (player != null)
{
player.GetComponent<Player>().OnBarHit();
}
}
}
}