Working collisions on event-based triggers
This commit is contained in:
@@ -4,8 +4,8 @@ using Pooling;
|
||||
namespace Minigames.DivingForPictures
|
||||
{
|
||||
/// <summary>
|
||||
/// Complete floating obstacle component that handles data, movement, collision detection, and pooling.
|
||||
/// Obstacles move upward toward the surface and detect collisions with the player.
|
||||
/// Complete floating obstacle component that handles data, movement, and pooling.
|
||||
/// Obstacles move upward toward the surface. Collision detection is now handled by the player.
|
||||
/// </summary>
|
||||
public class FloatingObstacle : MonoBehaviour, IPoolable
|
||||
{
|
||||
@@ -23,13 +23,6 @@ namespace Minigames.DivingForPictures
|
||||
[Tooltip("Whether this obstacle moves (can be disabled for static obstacles)")]
|
||||
[SerializeField] private bool enableMovement = true;
|
||||
|
||||
[Header("Collision Detection")]
|
||||
[Tooltip("Layer mask for player detection - should match Player layer")]
|
||||
[SerializeField] private LayerMask playerLayerMask = 1 << 7; // Player layer
|
||||
|
||||
[Tooltip("How often to check for collisions (in seconds)")]
|
||||
[SerializeField] private float collisionCheckInterval = 0.1f;
|
||||
|
||||
[Header("References")]
|
||||
[Tooltip("Reference to the spawner that created this obstacle")]
|
||||
[SerializeField] private ObstacleSpawner spawner;
|
||||
@@ -57,7 +50,6 @@ namespace Minigames.DivingForPictures
|
||||
|
||||
// Private fields
|
||||
private Collider2D _collider;
|
||||
private float _collisionCheckTimer;
|
||||
private bool _hasDealtDamage;
|
||||
private Camera _mainCamera;
|
||||
private float _screenTop;
|
||||
@@ -86,7 +78,6 @@ namespace Minigames.DivingForPictures
|
||||
HandleMovement();
|
||||
}
|
||||
|
||||
HandleCollisionDetection();
|
||||
CheckIfOffScreen();
|
||||
}
|
||||
|
||||
@@ -99,57 +90,17 @@ namespace Minigames.DivingForPictures
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks for collisions with the player at regular intervals
|
||||
/// Marks this obstacle as having dealt damage (called by PlayerDamageCollisionBehavior)
|
||||
/// </summary>
|
||||
private void HandleCollisionDetection()
|
||||
public void MarkDamageDealt()
|
||||
{
|
||||
if (_hasDealtDamage || _collider == null) return;
|
||||
|
||||
_collisionCheckTimer -= Time.deltaTime;
|
||||
if (_collisionCheckTimer <= 0f)
|
||||
if (!_hasDealtDamage)
|
||||
{
|
||||
_collisionCheckTimer = collisionCheckInterval;
|
||||
CheckForPlayerCollision();
|
||||
_hasDealtDamage = true;
|
||||
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} dealt {damage} damage to player");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if this obstacle is colliding with the player
|
||||
/// </summary>
|
||||
private void CheckForPlayerCollision()
|
||||
{
|
||||
Collider2D[] overlapping = new Collider2D[5];
|
||||
ContactFilter2D filter = new ContactFilter2D();
|
||||
filter.SetLayerMask(playerLayerMask);
|
||||
filter.useTriggers = true;
|
||||
|
||||
int count = _collider.Overlap(filter, overlapping);
|
||||
|
||||
if (count > 0 && !_hasDealtDamage)
|
||||
{
|
||||
// Found collision with player
|
||||
OnPlayerCollision(overlapping[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when this obstacle collides with the player
|
||||
/// </summary>
|
||||
/// <param name="playerCollider">The player's collider</param>
|
||||
private void OnPlayerCollision(Collider2D playerCollider)
|
||||
{
|
||||
_hasDealtDamage = true;
|
||||
|
||||
// Trigger damage through events (following the existing pattern)
|
||||
Debug.Log($"[FloatingObstacle] Obstacle dealt {damage} damage to player");
|
||||
|
||||
// Broadcast damage event using the static method
|
||||
PlayerCollisionBehavior.TriggerDamageStart();
|
||||
|
||||
// Continue moving upward - don't destroy or stop the obstacle
|
||||
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} hit player and continues moving");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the obstacle has moved off-screen and should be despawned
|
||||
/// </summary>
|
||||
@@ -202,7 +153,6 @@ namespace Minigames.DivingForPictures
|
||||
public void OnSpawn()
|
||||
{
|
||||
_hasDealtDamage = false;
|
||||
_collisionCheckTimer = 0f;
|
||||
_screenTop = 0f; // Reset cached screen bounds
|
||||
|
||||
// Ensure the obstacle is active and visible
|
||||
@@ -217,7 +167,6 @@ namespace Minigames.DivingForPictures
|
||||
public void OnDespawn()
|
||||
{
|
||||
_hasDealtDamage = false;
|
||||
_collisionCheckTimer = 0f;
|
||||
|
||||
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} despawned");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user