Hello
i’m working on my character stats.
I’m trying to deal damage to their energy when they move, using the left, right, up and down keys.
The problem is that Unity keeps multiplying by 100, instead of simply taking out energy every time the character moves. (-1 energy per frame for now, i will fix the deduction speed later) (or if you have any ideas those are welcomed.)
here is the code for the character stats script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterStats : MonoBehaviour
{
//a list of the various stats
public List<BaseStats> HappinessStats = new List<BaseStats>();
public List<BaseStats> EnergyStats = new List<BaseStats>();
public List<BaseStats> SmartsStats = new List<BaseStats>();
public List<BaseStats> LooksStats = new List<BaseStats>();
private int EnergyValue;
public int energyMinus;
public string walking;
void Start()
{
// set the values of each stat list
//HAPPINESS
HappinessStats.Add(new BaseStats(100, "Happiness: ", "Your Happiness level"));
//HappinessStats[0].AddStatBonus(new StatBonus(5));
HappinessStats[0].AddStatBonus(new StatBonus(-50));
Debug.Log("Happiness" + HappinessStats[0].GetCalculatedStatValue());
//ENERGY
EnergyStats.Add(new BaseStats((100), "Energy: ", "Your Energy level"));
Debug.Log("ENERGY" + EnergyStats[0].GetCalculatedStatValue());
//EnergyValue = EnergyStats[0].GetCalculatedStatValue();
//SMARTS
SmartsStats.Add(new BaseStats(100, "Smarts: ", "Your Intelligence level"));
//LOOKS
LooksStats.Add(new BaseStats(100, "Looks: ", "Your Looks level"));
}
void Update()
{
//have energy reduce as the character walks.
IsWalking();
}
//checks if the character is walking and returns a boolean
public void IsWalking()
{
if ((Input.GetAxis("Horizontal") != 0 && Input.GetAxis("Vertical") != 0) || (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0))
{
energyMinus += -1;
EnergyStats[0].RemoveStatBonus(new StatBonus(energyMinus));
Debug.Log(EnergyStats[0].GetCalculatedStatValue());
walking = "WE ARE WALKING";
}
else
{
energyMinus += 1;
EnergyStats[0].RemoveStatBonus(new StatBonus(energyMinus));
Debug.Log(EnergyStats[0].GetCalculatedStatValue());
walking = "WE ARE NOT WALKING";
}
}
}
here is the code for Base stats that creates a stat bonus list and adds or removes bonuses (walking damage)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseStats
{
public List<StatBonus> BaseAdditives { get; set; }
public int BaseValue { get; set; }
public string StatName { get; set; }
public string StatDescription{ get; set; }
public int FinalValue { get; set; }
public BaseStats(int baseValue, string statName, string statDescription)
{
//each stat, has an empty list of stat bonuses
this.BaseAdditives = new List<StatBonus>();
this.BaseValue = baseValue;
this.StatName = statName;
this.StatDescription = statDescription;
}
//ADD STAT BONUS FROM LIST
public void AddStatBonus(StatBonus statBonus)
{
this.BaseAdditives.Add(statBonus);
}
//REMOVE STAT BONUS FROM LIST
public void RemoveStatBonus(StatBonus statBonus)
{
this.BaseAdditives.Remove(statBonus);
}
//CALCULATE THE FINAL VALUE OF STAT
public int GetCalculatedStatValue()
{
this.BaseAdditives.ForEach(x => this.FinalValue += x.BonusValue);
FinalValue += BaseValue;
/*
for (int i = 0; i < BaseAdditives.Count; i++)
{
this.BaseAdditives.ForEach(x => this.FinalValue += x.BonusValue);
FinalValue += BaseValue;
}
*/
return FinalValue;
}
}