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
+7
View File
@@ -305,5 +305,12 @@ MonoBehaviour:
angrySound: {fileID: 8300000, guid: cc18c0832a7994c78bf33a2384a65f50, type: 3}
tiredSound: {fileID: 8300000, guid: bd98ba95bc1ff6961a95e12400cd71ce, type: 3}
deathSound: {fileID: 8300000, guid: e5a1c23ef78e24350938cbaa612817bb, type: 3}
hitSound: {fileID: 8300000, guid: 0535a1781b9d2e8018f56aa45b71932a, type: 3}
player: {fileID: 0}
gameBoardSpriteRenderer: {fileID: 0}
normalMaxSpeed: 2.5
turnRateDegreesPerSecond: 180
speedChangeRate: 4
targetReachedDistance: 0.15
alignmentThresholdDegrees: 5
targetTimeoutSeconds: 2
+43 -5
View File
@@ -1499,7 +1499,8 @@ GameObject:
- component: {fileID: 1002905790}
- component: {fileID: 1002905792}
- component: {fileID: 1002905791}
- component: {fileID: 1002905793}
- component: {fileID: 1002905794}
- component: {fileID: 1002905795}
m_Layer: 0
m_Name: Player
m_TagString: Player
@@ -1667,8 +1668,35 @@ MonoBehaviour:
magnetTop: {fileID: 797752166}
magnetBottom: {fileID: 1573512371}
ballSpawner: {fileID: 240489636}
--- !u!58 &1002905793
CircleCollider2D:
--- !u!50 &1002905794
Rigidbody2D:
serializedVersion: 5
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1002905789}
m_BodyType: 0
m_Simulated: 1
m_UseFullKinematicContacts: 0
m_UseAutoMass: 0
m_Mass: 1
m_LinearDamping: 0
m_AngularDamping: 0.05
m_GravityScale: 0
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_Constraints: 0
--- !u!61 &1002905795
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
@@ -1697,12 +1725,22 @@ CircleCollider2D:
m_CallbackLayers:
serializedVersion: 2
m_Bits: 4294967295
m_IsTrigger: 1
m_IsTrigger: 0
m_UsedByEffector: 0
m_CompositeOperation: 0
m_CompositeOrder: 0
m_Offset: {x: 0, y: 0}
m_Radius: 0.5
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
oldSize: {x: 0, y: 0}
newSize: {x: 0, y: 0}
adaptiveTilingThreshold: 0
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
m_Size: {x: 0.5, y: 0.5}
m_EdgeRadius: 0
--- !u!1 &1009138782
GameObject:
m_ObjectHideFlags: 0
+54 -1
View File
@@ -98,6 +98,8 @@ public class Player : MonoBehaviour
private float magnetTimer = 0.0f;
private bool killedByShark = false;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
@@ -148,8 +150,11 @@ public class Player : MonoBehaviour
}
// Update is called once per frame
void Update()
void FixedUpdate()
{
// Foce reset the rotation.
transform.rotation = Quaternion.identity;
if (damageTimer > 0.0f)
{
damageTimer -= Time.fixedDeltaTime;
@@ -233,6 +238,12 @@ public class Player : MonoBehaviour
gameOver = false; // Reset gameOver to prevent multiple coroutine starts
}
if (killedByShark)
{
killedByShark = false; // Reset the flag to prevent multiple sound plays.
StartCoroutine(Revive());
}
if (magnetTimer > 0.0f)
{
magnetTimer -= Time.deltaTime;
@@ -275,6 +286,29 @@ public class Player : MonoBehaviour
return false;
}
private IEnumerator Revive()
{
yield return new WaitForSeconds(1.0f);
if (lives > 0)
{
audioSource.PlayOneShot(killed);
yield return new WaitForSeconds(1.0f);
canFire = true;
exploding = false;
gunAnimator.SetTrigger("revived");
SpriteRenderer[] renderers = GetComponentsInChildren<SpriteRenderer>();
foreach (SpriteRenderer renderer in renderers)
{
renderer.enabled = true;
}
}
else
{
gameOver = true;
}
}
private IEnumerator EndGame()
{
musicManager.pauseMusic();
@@ -455,4 +489,23 @@ public class Player : MonoBehaviour
canFire = true; // Allow firing again after bar hit
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Shark"))
{
if (exploding)
{
return;
}
killedByShark = true;
lives--;
Explode();
}
else
{
Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true);
}
}
}
+1 -8
View File
@@ -17,13 +17,6 @@ public class PlayerGun : MonoBehaviour
public void AnimationFinished()
{
if (player.lives > 0)
{
animator.SetTrigger("revived");
}
else
{
spriteRenderer.enabled = false;
}
spriteRenderer.enabled = false;
}
}
+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();
}
}
}
}
@@ -34,3 +34,37 @@ MonoBehaviour:
- Key: LoadInBackGroundClipSizeThresholdBytes
Value: 204800
CurrentParamsIndex: 0
--- !u!114 &2
MonoBehaviour:
m_ObjectHideFlags: 53
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 0}
m_Name:
m_EditorClassIdentifier: UnityEditor.ProjectAuditorModule:Unity.ProjectAuditor.Editor:ProjectAuditorSettings
Rules:
rules: []
DiagnosticParams:
paramsStack:
- PlatformGroup:
m_String: Unknown
m_SerializedParams:
- Key: StreamingAssetsFolderSizeLimit
Value: 50
- Key: SpriteAtlasEmptySpaceLimit
Value: 50
- Key: TextureStreamingMipmapsSizeLimit
Value: 4000
- Key: StreamingClipThresholdBytes
Value: 218294
- Key: LongDecompressedClipThresholdBytes
Value: 204800
- Key: LongCompressedMobileClipThresholdBytes
Value: 204800
- Key: LoadInBackGroundClipSizeThresholdBytes
Value: 204800
CurrentParamsIndex: 0