As crazy as the title sounds, I want to add a ‘WeaponDamage’ variable to my ShieldConfig.cs script (I created that script when I was still developing my shield back then, to make it equipable), so I can give it it’s own unique damage when my player performs any sort of parrying with the shield (i.e: he strikes the enemy with the shield). How do I do this?
Right now, the parrying deals damage depending on what the player has in his attack slot, which is not ideal at all
Anyway, here’s my ‘ShieldConfig.cs’ script:
using System.Collections.Generic;
using UnityEngine;
using GameDevTV.Inventories;
using RPG.Stats;
namespace RPG.Combat {
[CreateAssetMenu(fileName = "Shield", menuName = "Defence/Shield", order = 0)]
public class ShieldConfig : EquipableItem, IModifierProvider
{
[SerializeField] Shield equippedPrefab;
[SerializeField] bool isRightHanded;
[SerializeField] float defenseBonus = 0;
[SerializeField] float percentageBonus = 0;
const string shieldName = "Shield";
public Shield SpawnEquipableItem(Transform rightHand, Transform leftHand) {
DestroyOldEquipableItem(rightHand, leftHand);
Shield shield = null;
if (equippedPrefab != null) {
Transform handTransform = GetTransform(rightHand, leftHand);
shield = Instantiate(equippedPrefab, handTransform);
shield.gameObject.name = shieldName;
}
return shield;
}
public float GetDefenseBonus()
{
return defenseBonus;
}
public float GetPercentageBonus()
{
return percentageBonus;
}
public Transform GetTransform(Transform rightHand, Transform leftHand) {
Transform handTransform;
if (isRightHanded) handTransform = rightHand;
else handTransform = leftHand;
return handTransform;
}
void DestroyOldEquipableItem(Transform rightHand, Transform leftHand) {
Transform oldWeapon = rightHand.Find(shieldName);
if (oldWeapon == null) {
oldWeapon = leftHand.Find(shieldName);
}
if (oldWeapon == null) return;
oldWeapon.name = "DESTROYING";
Destroy(oldWeapon.gameObject);
}
// Added Armor to 'Stats.cs'
public IEnumerable<float> GetAdditiveModifiers(Stat stat)
{
if (stat == Stat.Defence) yield return defenseBonus;
}
public IEnumerable<float> GetPercentageModifiers(Stat stat)
{
if (stat == Stat.Defence) yield return percentageBonus;
}
public override string GetDescription()
{
string result = "Protection Shield"; // definition
result += $"\n\n{GetRawDescription()}\n"; // description of the gameObject
if ((int) defenseBonus != 0) { // color change of bonus, based on positive or negative
string bonus = defenseBonus > 0 ? "<color=#8888ff>bonus</color>" : "<color=#ff8888>penalty</color>";
if ((int) defenseBonus >= 0) result += $"\n{(int)defenseBonus} point {bonus} to Defence";
else if ((int) defenseBonus < 0) result += $"\n{(int)defenseBonus * -1} point {bonus} to Defence";
}
if ((int) percentageBonus != 0) {
string bonus = percentageBonus > 0 ? "<color=#8888ff>bonus</color>" : "<color=#ff8888>penalty</color>";
if ((int) percentageBonus >= 0) result += $"\n{(int)percentageBonus} percentage {bonus} to Defence";
else if ((int) percentageBonus < 0) result += $"\n{(int) percentageBonus * -1} percentage {bonus} to Defence";
}
result += isRightHanded ? "\nshield is right handed" : "\nshield is left handed";
return result;
}
}
}