MiniGames/Assets/Scripts/SkyWalker/Skywalker_BounceObstacle.cs
2025-09-06 23:30:06 +05:00

38 lines
1.4 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
Skywalker_PlayerController playerController;
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"))
{
if(playerController == null) {playerController=collision.gameObject.GetComponent<Skywalker_PlayerController>();};
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);
//playerController.animator.SetBool("IsGrounded", false);
playerController.animator.SetTrigger("Jump");
Debug.Log("Player bounced!");
}
}
}
}