- Home /
Question by
jonathanbooker2911 · Dec 03, 2018 at 07:45 PM ·
scripting problemscript.scripting beginnerscriptingbasicsscript error
How do I make do I make a variable go back to its original value for a spell system
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class HumanRacial : MonoBehaviour
{
public float agility;
public float strength;
public float intellect;
public float coolDown = 120;
public float coolDownTimer;
public float duration;
public float durationTimer;
public bool isWarrior;
public bool isBountyHunter;
public bool isMage;
public void Update()
{
Active();
Cooldown();
Wakeup();
}
public void RacialBonus()
{
if(isMage == true)
{
intellect += intellect;
}
if (isWarrior == true)
{
strength += strength;
}
if (isBountyHunter == true)
{
agility += agility;
}
}
private void Cooldown()
{
if (duration > 0)
{
durationTimer -= Time.deltaTime;
}
if (durationTimer < 0)
{
durationTimer = 0;
}
if (coolDownTimer > 0)
{
coolDownTimer -= Time.deltaTime;
}
if (coolDownTimer < 0)
{
coolDownTimer = 0;
}
}
void Wakeup()
{
if (durationTimer <= 0.1f && isWarrior == true)
{
strength = strength / 2f;
}
if (durationTimer <= 0 && isMage == true)
{
intellect = intellect / 2f;
}
if (durationTimer <= 0 && isBountyHunter == true)
{
agility = agility / 2f;
}
}
private void Active()
{
if (Input.GetKeyDown(KeyCode.D) && coolDownTimer == 0 && durationTimer == 0)
{
coolDownTimer = coolDown;
durationTimer = duration;
RacialBonus();
}
}
}
How do I make the variable go back to normal after durationTimer hits zero.
Comment
Answer by Vega4Life · Dec 03, 2018 at 08:50 PM
This may be a good time to start using some scriptable objects. This allows you to store data separately for each racial type you have, and it also allows you to easily revert back to base data for each racial.
Here is some code for getting the idea across:
using UnityEngine;
public class HumanRacial : MonoBehaviour
{
// Racial is a scriptabl object, holding all your racial data for warrior, mage, bounty hunter
[SerializeField] Racial racial;
float currentAgility;
float currentStrength;
float currentIntellect;
float coolDownTimer;
float durationTimer;
void Awake()
{
Setup();
}
void Setup()
{
currentAgility = racial.agility;
currentIntellect = racial.intellect;
currentStrength = racial.strength;
}
public void Update()
{
Active();
Cooldown();
Wakeup();
}
public void RacialBonus()
{
if (racial.racialType == Racial.RacialType.Mage)
{
currentIntellect += currentIntellect;
}
if (racial.racialType == Racial.RacialType.Warrior)
{
currentStrength += currentStrength;
}
if (racial.racialType == Racial.RacialType.BountyHunter)
{
currentAgility += currentAgility;
}
}
private void Cooldown()
{
if (racial.duration > 0)
{
durationTimer -= Time.deltaTime;
}
if (durationTimer < 0)
{
durationTimer = 0;
}
if (coolDownTimer > 0)
{
coolDownTimer -= Time.deltaTime;
}
if (coolDownTimer < 0)
{
coolDownTimer = 0;
}
}
void Wakeup()
{
if (durationTimer <= 0.1f && racial.racialType == Racial.RacialType.Warrior)
{
currentStrength = racial.strength;
}
if (durationTimer <= 0 && racial.racialType == Racial.RacialType.Mage)
{
currentIntellect = racial.intellect;
}
if (durationTimer <= 0 && racial.racialType == Racial.RacialType.BountyHunter)
{
currentAgility = racial.agility;
}
}
private void Active()
{
if (Input.GetKeyDown(KeyCode.D) && coolDownTimer == 0 && durationTimer == 0)
{
coolDownTimer = racial.coolDown;
durationTimer = racial.duration;
RacialBonus();
}
}
}
Here is the scriptable for your data:
using UnityEngine;
// Scriptable to hold all your racial data for each class
// Each class can have its seperate dat
// Create a racial by going to assets -> create -> HumanRacial
[CreateAssetMenu(menuName = "HumanRacial/Racial")]
public class Racial : ScriptableObject
{
public enum RacialType { Warrior, Mage, BountyHunter };
public RacialType racialType;
public float agility;
public float strength;
public float intellect;
public float coolDown;
public float duration;
}
This type of system makes it easy to interchange racials, get the correct data, etc. Nothing touches the racials base data, so its easy to reset.
I have new problem now floats post numbers like 1.50008 but I need it to hit 1f for the stat change. Any idea of a way around that.