Реализованы классы Damager и Hitbox

This commit is contained in:
Сергей Бондарь 2026-03-31 13:27:10 +03:00
parent 8b7b8830fc
commit 9ecc66aea1
5 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 14fb34962344db448b981ce94efdde4d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,32 @@
using UnityEngine;
using UnityEngine.Events;
[RequireComponent (typeof(Collider))]
public class Damager : MonoBehaviour
{
[SerializeField] private int Damage;
private GameObject Owner;
public UnityEvent OnCollision;
public void Initialize(GameObject Owner)
{
if (OnCollision == null) OnCollision = new();
this.Owner = Owner;
}
private void OnTriggerEnter(Collider other)
{
if(other.TryGetComponent<Hitbox>(out var hitbox))
{
//Коллизия с собой
if (hitbox.Owner == this.Owner) return;
hitbox.TakeDamage(Damage);
}
OnCollision?.Invoke();
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2b9c4414025683b40ace934a0c1be3f5

View File

@ -0,0 +1,11 @@
using System;
using UnityEngine;
[RequireComponent (typeof(Collider))]
public class Hitbox : MonoBehaviour
{
public GameObject Owner { get; private set; }
public event Action<int> OnDamage;
public void Initialize(GameObject Owner) => this.Owner = Owner;
public void TakeDamage(int damage) => OnDamage?.Invoke(damage);
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fc7a89df164d2684b9953c773b371384