fgm24/Assets/Scripts/Player/PlayerCollideAttack.cs

28 lines
913 B
C#
Raw Normal View History

2024-02-04 08:27:38 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCollideAttack : MonoBehaviour
{
[SerializeField] Rigidbody2D body;
[SerializeField] LayerMask damageLayers;
[SerializeField] AnimationCurve speedToDamage;
public void OnCollisionEnter2D(Collision2D collision)
{
2024-02-04 09:24:51 +01:00
if (collision.collider.tag != "Enemy") return;
2024-02-04 08:27:38 +01:00
HealthComponent health = collision.collider.gameObject.GetComponent<HealthComponent>();
if (health == null)
health = collision.collider.transform.parent.GetComponent<HealthComponent>();
if (health == null) return;
2024-02-04 09:24:51 +01:00
health = collision.collider.gameObject.GetComponentInChildren<HealthComponent>();
if (health == null) return;
2024-02-04 08:27:38 +01:00
float speed = body.velocity.magnitude;
float damage = speedToDamage.Evaluate(speed);
health.TakeDamage(damage);
}
}