Thank you for your answers. To complete some of your questions, this is the way I check collisions, I don’t know if it’s the best way but I find it very useful for now and quite simple.
I made a interface called OnHit:
using UnityEngine;
public interface IOnHit<T> {
void OnHit(T obj);
}
Then in the player (human) class, I have added the OnTriggerEnter function like that:
public class Human : MonoBehaviour, IOnHit<WeaponMelee>, IOnHit<Projectile> {
// So when something hit the human, it checks if the other implements the IOnHit interface of its class in this case, Human class
protected void OnTriggerEnter(Collider other)
{
IOnHit<Human> hit = other.gameObject.GetComponent<IOnHit<Human>>();
hit?.OnHit(this);
}
public void OnHit(WeaponMelee weaponMelee)
{
Damage(weaponMelee);
}
public void OnHit(Projectile projectile)
{
Damage(projectile);
}
}
I have done the same thing with the shield collider and weapon collider :
public class ShieldCollider : Collider<Shield>, IOnHit<WeaponMelee>, IOnHit<Projectile> {
public ParticleSystem particles;
#region OnHit
protected override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
IOnHit<Shield> hit = other.gameObject.GetComponent<IOnHit<Shield>>();
hit?.OnHit(item);
}
public void OnHit(WeaponMelee weaponMelee)
{
item.InstantiateHolderEffects();
item.InstantiateEntityEffects(weaponMelee.holder);
particles?.Play();
item.holder.PlayAnimation("TakeAttack");
item.holder.stamina.Remove(weaponMelee.weight, false);
item.RemoveDurability(weaponMelee);
if (item.durability.value <= 0)
item.holder.UnequipShield();
}
public void OnHit(Projectile projectile)
{
item.InstantiateHolderEffects();
item.InstantiateEntityEffects(projectile.caster);
particles?.Play();
item.holder.PlayAnimation("TakeAttack");
}
#endregion
}
public class WeaponMeleeCollider : Collider<WeaponMelee>, IOnHit<Human>, IOnHit<Shield> {
public ParticleSystem particles;
#region OnHit
protected override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
IOnHit<WeaponMelee> hit = other.gameObject.GetComponent<IOnHit<WeaponMelee>>();
hit?.OnHit(item);
}
public void OnHit(Human human)
{
item.InstantiateHolderEffects();
item.InstantiateEntityEffects(human);
item.RemoveDurability(weaponDamages);
if (item.durability.value <= 0)
item.holder.UnequipWeapon();
}
public void OnHit(Shield shield)
{
item.InstantiateHolderEffects();
item.InstantiateEntityEffects(shield.holder);
item.collider.Enable(false);
particles?.Play();
}
#endregion
}
For the particles, I have added a public variable where I put the particules system in the prefab and play it depend on what the collider hit.
The reason I did this, is because it allows me to custom particles for each weapon and shield since some of them are very differents which a particulesManager can’t. And I can manage this in the weapon or shield prefab instead of code which is easier in my opinion.
Maybe there is a better way.
I hop it’s clear enough to understand what I have done.
If you have any suggestion, I would happy to read them.