MVP of the plane throwing game (#77)
Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com> Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com> Reviewed-on: #77
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Interactive
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies a constant force to airplanes passing through this zone.
|
||||
/// Can be used for updrafts, downdrafts, or crosswinds.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class AirplaneWindZone : MonoBehaviour
|
||||
{
|
||||
[Header("Wind Configuration")]
|
||||
[SerializeField] private Vector2 windForce = new Vector2(0, 5f);
|
||||
[SerializeField] private bool isWorldSpace = true;
|
||||
|
||||
[Header("Visual Feedback (Optional)")]
|
||||
[SerializeField] private ParticleSystem windParticles;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Ensure collider is trigger
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
collider.isTrigger = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerStay2D(Collider2D other)
|
||||
{
|
||||
// Check if it's an airplane
|
||||
var airplane = other.GetComponent<Core.AirplaneController>();
|
||||
if (airplane == null || !airplane.IsFlying) return;
|
||||
|
||||
// Apply wind force
|
||||
var rb = other.GetComponent<Rigidbody2D>();
|
||||
if (rb != null)
|
||||
{
|
||||
Vector2 force = isWorldSpace ? windForce : transform.TransformDirection(windForce);
|
||||
rb.AddForce(force * Time.fixedDeltaTime, ForceMode2D.Force);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Debug.Log($"[AirplaneWindZone] Applied force: {force} to {other.name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// Visualize wind direction in editor
|
||||
Gizmos.color = windForce.y > 0 ? Color.green : Color.red;
|
||||
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
Vector3 center = collider.bounds.center;
|
||||
Vector2 direction = isWorldSpace ? windForce : transform.TransformDirection(windForce);
|
||||
|
||||
// Draw arrow showing wind direction
|
||||
Gizmos.DrawRay(center, direction.normalized * 2f);
|
||||
Gizmos.DrawWireSphere(center + (Vector3)(direction.normalized * 2f), 0.3f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user