Danro/Assets/Scripts/Behaviour/JumpPad2D.cs

97 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using UnityEngine;
namespace RR.Game2D.Behaviour
{
[RequireComponent(typeof(BoxCollider2D))]
public class JumpPad2D : MonoBehaviour
{
[SerializeField] private float verticalBand = 0.5f;
private BoxCollider2D _col;
public Action<GameObject> PlayerJumped;
private void Awake()
{
_col = GetComponent<BoxCollider2D>();
if (!_col.isTrigger)
_col.isTrigger = true;
}
//private void OnTriggerEnter2D(Collider2D other)
//{
// var d = col.Distance(other);
// Vector2 contactMid = (d.pointA + d.pointB) * 0.5f;
// bool topByNormal = IsTopByNormal(col, d.normal, maxAngleDeg: 30f);
// bool topByBox = false;
// if (col is BoxCollider2D box)
// topByBox = IsTopByLocalY(box, contactMid, bandPercent: bandPercent);
// bool isTopContact = topByNormal || topByBox;
// if (isTopContact)
// {
// if (other.CompareTag("Player"))
// {
// PlayerController playerController = other.GetComponent<PlayerController>();
// Rigidbody2D playerRB = other.GetComponent<Rigidbody2D>();
// Vector2 newPlayerVelocity = playerRB.linearVelocity;
// newPlayerVelocity.y = playerController.jumpVelocity;
// playerRB.linearVelocity = newPlayerVelocity;
// }
// }
// //var distance = col.Distance(collision);
// //Vector2 localPoint = col.transform.InverseTransformPoint(distance.pointA);
// //float halfHeight = col.size.y * 0.5f;
// //float topY = col.offset.y * halfHeight;
// //float band = Mathf.Max(0.001f, col.size.y * bandPercent);
//}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
PlayerController playerController = other.GetComponent<PlayerController>();
Rigidbody2D playerRB = other.GetComponent<Rigidbody2D>();
if (Mathf.Abs(other.transform.position.y - _col.bounds.max.y) <= verticalBand)
{
Vector2 newPlayerVelocity = playerRB.linearVelocity;
newPlayerVelocity.y = playerController.jumpVelocity;
playerRB.linearVelocity = newPlayerVelocity;
PlayerJumped?.Invoke(other.gameObject);
}
}
}
//public static bool IsTopByNormal(Collider2D myCol, Vector2 contactNormal, float maxAngleDeg = 30f)
//{
// var up = (Vector2)myCol.transform.up;
// float cos = Vector2.Dot(contactNormal.normalized, up);
// float cosThresh = Mathf.Cos(maxAngleDeg * Mathf.Deg2Rad);
// return cos >= cosThresh;
//}
//public static bool IsTopByLocalY(BoxCollider2D box, Vector2 worldPoint, float bandPercent = 0.1f)
//{
// // Локальная точка
// Vector2 pLocal = box.transform.InverseTransformPoint(worldPoint);
// // Верхняя кромка в локальных координатах с учётом offset
// float halfH = box.size.y * 0.5f;
// float topY = box.offset.y + halfH;
// // Ширина допустимого пояска (процент от высоты коробки)
// float band = Mathf.Max(0.001f, box.size.y * bandPercent);
// return pLocal.y >= (topY - band);
//}
}
}