TO SQUASH: Fixed collision issues with the shark.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user