feat: Enemies
This commit is contained in:
@@ -3,7 +3,8 @@ using UnityEngine;
|
||||
namespace Arena.Combat
|
||||
{
|
||||
/// <summary>
|
||||
/// Moves straight ahead and damages the first IDamageable it touches.
|
||||
/// Moves straight ahead and damages the first IDamageable of the targeted team it touches.
|
||||
/// It goes through the other teams: no friendly fire.
|
||||
/// Needs a trigger collider and a kinematic Rigidbody.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
@@ -36,12 +37,21 @@ namespace Arena.Combat
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
// Ignore the shooter and the other trigger zones
|
||||
if (other.isTrigger || other.gameObject == _damage.Source)
|
||||
// Ignore the other trigger zones, and the shooter with all its children
|
||||
if (other.isTrigger || IsFromShooter(other))
|
||||
return;
|
||||
|
||||
if (other.TryGetComponent(out IDamageable target))
|
||||
// The collider can be on a child of the IDamageable: arm, head, model...
|
||||
IDamageable target = other.GetComponentInParent<IDamageable>();
|
||||
|
||||
if (target != null)
|
||||
{
|
||||
// A friend is crossed, not hit
|
||||
if (!_damage.CanHit(target.Team))
|
||||
return;
|
||||
|
||||
target.TakeDamage(_damage);
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
@@ -61,5 +71,15 @@ namespace Arena.Combat
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private bool IsFromShooter(Collider other)
|
||||
{
|
||||
GameObject shooter = _damage.Source;
|
||||
return shooter && other.transform.IsChildOf(shooter.transform);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user