65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Collider))]
|
|
public class Skywalker_FrontFan : MonoBehaviour
|
|
{
|
|
[Header("Fan Settings")]
|
|
public float maxPushForce = 30f; // strongest wind at fan's start
|
|
public float minPushForce = 5f; // weakest wind near the end
|
|
public float counterForceMultiplier = 3f; // extra push if running into wind
|
|
public bool debugGizmo = true;
|
|
|
|
private BoxCollider fanCollider;
|
|
|
|
private void Awake()
|
|
{
|
|
fanCollider = GetComponent<BoxCollider>();
|
|
if (fanCollider != null) fanCollider.isTrigger = true;
|
|
}
|
|
|
|
private void OnTriggerStay(Collider col)
|
|
{
|
|
if (!col.CompareTag("Player")) return;
|
|
|
|
Rigidbody rb = col.attachedRigidbody;
|
|
if (rb == null) return;
|
|
|
|
Vector3 windDir = transform.up.normalized;
|
|
|
|
// Calculate how far player is inside the fan tunnel (0 = near fan, 1 = far end)
|
|
float localZ = transform.InverseTransformPoint(col.transform.position).z;
|
|
float halfDepth = fanCollider.size.z * 0.5f;
|
|
float t = Mathf.InverseLerp(-halfDepth, halfDepth, localZ);
|
|
|
|
// Interpolate force based on distance (closer = stronger)
|
|
float pushForce = Mathf.Lerp(maxPushForce, minPushForce, t);
|
|
|
|
Vector3 force = windDir * pushForce;
|
|
|
|
// If player is moving against the wind, apply stronger push
|
|
float dot = Vector3.Dot(rb.velocity.normalized, -windDir);
|
|
if (dot > 0.5f) // player moving forward into wind
|
|
{
|
|
force += windDir * (pushForce * (counterForceMultiplier - 1));
|
|
}
|
|
|
|
// Apply only horizontal push
|
|
rb.AddForce(new Vector3(force.x, 0f, force.z), ForceMode.Acceleration);
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
if (!debugGizmo) return;
|
|
|
|
BoxCollider col = GetComponent<BoxCollider>();
|
|
if (col == null) return;
|
|
|
|
Gizmos.color = Color.blue;
|
|
Gizmos.DrawWireCube(transform.position + col.center, col.size);
|
|
|
|
// Wind direction arrow
|
|
Gizmos.color = Color.cyan;
|
|
Gizmos.DrawRay(transform.position, transform.up * 2f);
|
|
}
|
|
}
|