- Home /
Increasing or decreasing int values quickly?
I'm making an RTS game where resources are gathered and taken away, when gaining or spending resources I would like to increase or decrease them quickly instead of taking them or adding them all at one shot. The resources are int values since I don't want decimals in my resources. Is there an easy way to do this? Thanks!!
Answer by Pengocat · Dec 18, 2016 at 12:22 AM
I put this together that try to go for the resourcesTarget by lerping on a curve that slows down when close to the target value. Just for previewing the resourcesTarget is Serialized so it can be changed in the inspector but it should work better if set using the ResourcesTarget Property. You can also play with the lerpDelay to control the time to reach the target.
[Range(0, 1000000)]
[SerializeField]
private int resourcesTarget = 100000;
[Range(0.1f, 20f)]
public float lerpDelay = 2f;
public int resourcesDisplayed;
Text resourcesTextBox;
string resourcesText;
bool isTargetChanged;
public int ResourcesTarget
{
get
{
return resourcesTarget;
}
set
{
resourcesTarget = value;
isTargetChanged = true;
}
}
void Start()
{
StartCoroutine(ResourcesCorrection());
resourcesTextBox = GetComponent<Text>();
}
void Update()
{
if (resourcesTextBox.text != resourcesText)
{
resourcesTextBox.text = resourcesText;
}
}
IEnumerator ResourcesCorrection()
{
while (true)
{
if (resourcesTarget == resourcesDisplayed)
{
yield return null;
}
else
{
int resourcesOld = resourcesDisplayed;
isTargetChanged = false;
float currentLerpTime = 0f;
while (resourcesTarget != resourcesDisplayed && isTargetChanged == false)
{
currentLerpTime += Time.deltaTime;
if (currentLerpTime > lerpDelay)
{
currentLerpTime = lerpDelay;
}
float t = currentLerpTime / lerpDelay;
t = Mathf.Sin(t * Mathf.PI * 0.5f);
resourcesDisplayed = (int)Mathf.Lerp(resourcesOld, resourcesTarget, t);
resourcesText = resourcesDisplayed.ToString();
yield return null;
}
}
}
}
This works great thanks!! Quick question though, im think about putting this on every building I can place also so that I can change the lerp time to the amount of time the building takes to build so that it takes resources away while its building would the above script work well with that too?
Yes it could work. You could lerp the cost per building separately. So if the building cost 2000 you lerp from 0-2000 and the lerpDelay = buildtime. $$anonymous$$eanwhile you also subtract the build cost from the total resources pool. You could remove the $$anonymous$$athf.Sin line to get a linear curve.
If the LerpTime (Lerpdelay) equals the buildingTime of the building the resources being taken away lines up with the building completing its build perfectly. I don't want have to keep changing the Lerptime to the buildingTime so I can have the building complete its build and the resources for that building to stop being taken away at the same time. Any Solution to this?
Wouldnt this be difficult when multiple buildings are building because then the lerptime will be jumping all around depending on different build times @Pengocat
You could run multiple coroutines with different payAmount and buildTime.
public int totalResources = 10000;
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
StartCoroutine(PayResourcesDuringBuild(2000, 5f));
}
}
IEnumerator PayResourcesDuringBuild(int payAmount, float buildTime)
{
int currentAmount = 0;
int subtract = 0;
float currentLerpTime = 0f;
int alreadySubtracted = 0;
while (payAmount != currentAmount)
{
currentLerpTime += Time.deltaTime;
if (currentLerpTime > buildTime)
{
currentLerpTime = buildTime;
}
float t = currentLerpTime / buildTime;
currentAmount = (int)$$anonymous$$athf.Lerp(0, payAmount, t);
subtract = currentAmount - alreadySubtracted;
totalResources -= subtract;
alreadySubtracted += subtract;
yield return null;
}
}