oh come on though… you know I bring up some great ideas
sure, here you go:
// New Code, for Third Person projectiles:
private void OnTriggerEnter(Collider other)
{
// This function tells the projectile holding this script what to do
// when it hits something, depending on whether it's a target, or just
// something in the way
if (other.gameObject == instigator) return;
if (other.TryGetComponent(out Health health)) health.TakeDamage(instigator, damage, instigator.GetComponent<Fighter>().GetCurrentWeaponConfig().GetSkill());
if (other.TryGetComponent(out ForceReceiver forceReceiver)) forceReceiver.AddForce(transform.forward * damage * 0.5f, true); // multiplied by 0.5f so as to neutralize the knockback impact
speed = 0;
onHit.Invoke();
if (hitEffect != null) Instantiate(hitEffect, transform.position, transform.rotation);
foreach (GameObject toDestroy in destroyOnHit) Destroy(toDestroy);
Destroy(gameObject, lifeAfterImpact);
Debug.Log($"Instigator = {instigator.name}");
Debug.Log($"Collider = {other.name}");
Debug.Log($"Damage Dealt = {damage}");
}
voila:
void TryHit(int slot)
{
// if no current attack (or follow up) exists, return null:
if (currentAttack == null) return;
// radius of the damage of the weapon:
float damageRadius = 0.5f;
// To trigger this animation, the event in the animation takes the 1 and 2 values in 'Int' slot
Vector3 transformPoint;
switch (slot)
{
case 0: transformPoint = currentWeapon.value.DamagePoint; // weapon damage
damageRadius = currentWeapon.value.DamageRadius;
break;
case 1: transformPoint = rightHandTransform.position; // for right-handed cases
break;
case 2: transformPoint = leftHandTransform.position; // for left-handed cases
break;
// case 3: for right foot, but after you got the animation intact and the transform setup
// case 4: for left foot. Again, after the animation is intact and the transform is setup
default: transformPoint = rightHandTransform.position; // for cases when things make no sense
break;
}
Debug.Log($"Attacking with slot {slot}, position {transformPoint}");
foreach (Collider other in Physics.OverlapSphere(transformPoint, damageRadius))
{
if (other.gameObject == gameObject) continue; // don't hit yourself
if (other.TryGetComponent(out Health otherHealth) && !otherHealth.IsDead())
{
float damage = GetDamage();
damage *= currentAttack.DamageModifier;
// player/enemy
if (other.TryGetComponent(out BaseStats otherBaseStats))
{
float defence;
// if its the player (the only in-game character with a skillStore)
if (other.TryGetComponent(out SkillStore skillStore))
{
defence = otherBaseStats.GetStatBySpecifiedLevel(Stat.Defence, skillStore.GetSkillLevel(Skill.Defence));
}
// the enemy
else defence = otherBaseStats.GetStat(Stat.Defence);
damage /= 1 + defence/damage;
// damage *= UnityEngine.Random.Range(0f, 1.25f);
// if (UnityEngine.Random.Range(0,100) > 95) damage *= 2.0f;
}
if (other.tag == "Player") // (blocking state) or you can try get the SkillStore, either works...!
{
bool inVulnerable = other.GetComponent<Health>().GetInvulnerable();
if (inVulnerable) return; // add more information in here, to accept up to 50% original damage (use Random.Range for that), and impact state for example (the better the shield, the less likely it is to allow damage through)
}
if (otherHealth.IsDead()) return; // can't hit a dead enemy
otherHealth.TakeDamage(gameObject, damage, currentWeaponConfig.GetSkill());
TryApplyHitForce(other, transform.position);
}
}
}
and ‘Shoot()’, if you need it:
// Called on the ranged animation event lines:
void Shoot() {
// In very simple terms, this function is the 'Hit()' function for
// Ranged (Projectile) Weapons, to be called in 'Projectile.cs' (for simplicity in naming's sake)
// Hit();
// RPG to Third Person Conversion changes are what the rest of this function is all about:
if (!currentWeaponConfig.HasProjectile()) return;
if (TryGetComponent(out ITargetProvider targetProvider))
{
float damage = GetDamage();
GameObject targetObject = targetProvider.GetTarget();
if (targetObject != null)
{
// use the 'LaunchProjectile' function with 5 arguments (as we do have a target)
currentWeaponConfig.LaunchProjectile(rightHandTransform, leftHandTransform, targetObject.GetComponent<Health>(), gameObject, damage);
}
else
{
// use the 'LaunchProjectile' function with 4 arguments (as we don't have a target)
currentWeaponConfig.LaunchProjectile(rightHandTransform, leftHandTransform, gameObject, damage);
}
// If you have enough ammo, fire and use the ammo
if (currentWeaponConfig.GetAmmunitionItem())
{
GetComponent<Quiver>().SpendAmmo(1);
}
}
}
Side question, how do I move my project safely to another drive on my computer? As we speak, my SSD is running low on space and performance is becoming absurdly slow (I’m not talking about Unity packages, I’m talking about the project itself, literally)