92 lines
2.7 KiB
C#
92 lines
2.7 KiB
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Collider))]
|
|
public class Skywalker_FanLauncher : MonoBehaviour
|
|
{
|
|
[Header("Fan Settings")]
|
|
public float hoverHeight = 5f; // world Y position where player should hover
|
|
public float ascendSpeed = 5f; // how fast the player rises to hoverHeight
|
|
public float hoverDamp = 0.1f; // how smoothly velocity damps near hover
|
|
public bool debugGizmo = true;
|
|
|
|
private bool playerInside = false;
|
|
Skywalker_PlayerController playerController;
|
|
void Reset()
|
|
{
|
|
Collider col = GetComponent<Collider>();
|
|
if (col != null)
|
|
col.isTrigger = true;
|
|
}
|
|
|
|
void OnTriggerEnter(Collider col)
|
|
{
|
|
if (col.CompareTag("Player"))
|
|
{
|
|
Rigidbody rb = col.attachedRigidbody;
|
|
if (playerController == null) { playerController = col.gameObject.GetComponent<Skywalker_PlayerController>(); };
|
|
|
|
if (rb != null)
|
|
{
|
|
playerController.animator.SetTrigger("Jump");
|
|
|
|
rb.useGravity = false; // disable gravity inside fan
|
|
playerInside = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnTriggerStay(Collider col)
|
|
{
|
|
if (col.CompareTag("Player"))
|
|
{
|
|
Rigidbody rb = col.attachedRigidbody;
|
|
|
|
if (rb != null)
|
|
{
|
|
Vector3 vel = rb.velocity;
|
|
|
|
// If below hover height → push upward
|
|
if (rb.position.y < hoverHeight - 0.2f)
|
|
{
|
|
vel.y = ascendSpeed;
|
|
}
|
|
// If at/above hover height → damp velocity to hover
|
|
else
|
|
{
|
|
vel.y = Mathf.Lerp(vel.y, 0f, hoverDamp);
|
|
rb.position = new Vector3(rb.position.x, Mathf.Lerp(rb.position.y, hoverHeight, 0.05f), rb.position.z);
|
|
}
|
|
|
|
rb.velocity = vel;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnTriggerExit(Collider col)
|
|
{
|
|
if (col.CompareTag("Player"))
|
|
{
|
|
Rigidbody rb = col.attachedRigidbody;
|
|
if (rb != null)
|
|
{
|
|
rb.useGravity = true; // restore normal gravity
|
|
playerInside = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnDrawGizmos()
|
|
{
|
|
if (!debugGizmo) return;
|
|
|
|
Gizmos.color = Color.green;
|
|
Gizmos.DrawWireCube(transform.position, GetComponent<Collider>().bounds.size);
|
|
|
|
// Draw hover height line
|
|
Gizmos.color = Color.yellow;
|
|
Gizmos.DrawLine(new Vector3(transform.position.x - 2, hoverHeight, transform.position.z),
|
|
new Vector3(transform.position.x + 2, hoverHeight, transform.position.z));
|
|
}
|
|
}
|