MiniGames/Assets/Skywalker_BounceObstacle.cs
2025-09-01 00:01:33 +05:00

36 lines
1.1 KiB
C#

using UnityEngine;
[RequireComponent(typeof(MeshCollider))]
public class Skywalker_BounceObstacle : MonoBehaviour
{
[Header("Bounce Settings")]
public float bounceForce = 15f; // how strong the bounce is
public bool resetYVelocity = true; // reset vertical velocity before bounce
private void Reset()
{
Collider col = GetComponent<Collider>();
if (col != null)
col.isTrigger = false; // solid collider by default
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Rigidbody rb = collision.gameObject.GetComponent<Rigidbody>();
if (rb != null)
{
// Optionally reset Y velocity so bounce is consistent
if (resetYVelocity)
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
// Apply bounce force
rb.AddForce(Vector3.up * bounceForce, ForceMode.VelocityChange);
Debug.Log("Player bounced!");
}
}
}
}