Danro/Assets/Scripts/Entities/JumpPad2D.cs

41 lines
1.1 KiB
C#

using System;
using UnityEngine;
using RR.Game2D.Behaviour;
namespace RR.Game2D.Entities
{
[RequireComponent(typeof(BoxCollider2D))]
public class JumpPad2D : MonoBehaviour
{
[SerializeField] private float verticalBand = 0.75f;
private BoxCollider2D _col;
public Action<GameObject> PlayerJumped;
private void Awake()
{
_col = GetComponent<BoxCollider2D>();
if (!_col.isTrigger)
_col.isTrigger = true;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (!other.CompareTag("Player")) return;
PlayerController playerController = other.GetComponent<PlayerController>();
Rigidbody2D playerRB = other.GetComponent<Rigidbody2D>();
if (!(Mathf.Abs(other.transform.position.y - _col.bounds.max.y) <= verticalBand)) return;
Vector2 newPlayerVelocity = playerRB.linearVelocity;
newPlayerVelocity.y = playerController.jumpVelocity;
playerRB.linearVelocity = newPlayerVelocity;
PlayerJumped?.Invoke(other.gameObject);
}
}
}