- Home /
Synchronize changing values (that go in opposite directions)
I am trying to work out a way to sync all my value systems together so as one changes, it influences the others.
Here is what I have: When a player holds down a button, their attack meter goes up. This is clamped so that it cannot go above maxAttackValue. As the character levels up, the maxAttackValue can increment.
Running in counter to this, I have a particle system from particle playground that I have doing effects. Because it works on the particle playground system, I can't change the way that the particle mask behaves. It works in an opposite way where I need the value to decrease. The starting value for this is 550 and the lowest will be maxParticleMaskValue. maxParticleValue will increase as levels go up (but the value will be getting lower).
For example Character at level 1:
minAttackValue 0;
maxAttackValue 10;
minParticleMaskValue = 550;
maxParticleMaskValue = 400;
Character at level 2:
minAttackValue 5;
maxAttackValue 20;
minParticleMaskValue = 500;
maxParticleMaskValue = 300;
Character at level 10:
minAttackValue 10;
maxAttackValue 50;
minParticleMaskValue = 300;
maxParticleMaskValue = 0;
Basically I need it so when the player holds the button, the currentAttackValue increases until it reaches max. At the same time currentParticleValue needs to decrease and reach maxTime at the same time that attackValue does.
After writing it out, it does sound confusing. Ha.
void Update(){
curAttack = Mathf.Clamp(curAttack,minAttackValue,maxAttackValue);
curParticle = Mathf.Clamp(curParticle,minParticleValue,maxParticleValue);
if(Input.GetKey(KeyCode.C))
{
attackState = Attack.Increasing;
particleState = Particle.Decreasing;
}
if(attackState == Attack.Increasing)
{
curAttack += Time.deltaTime; //These values need to work in opposite but
curParticle -= Time.deltaTime; //arrive at their respective max values at the same time
}
else
{
attackValue -= Time.deltaTime;
curParticle -= Time.deltaTime;
}
If only unity had some king of mathf.RunInOpposite. Ha.
Thanks in advance to those with better maths skills than I.
Since Particle Playground 2.2 there's a mask sorting option, great you worked around the issue nonetheless!
Answer by taxvi · Dec 12, 2014 at 08:50 AM
try this:
curParticle = ((curAttack - minAttack) / (maxAttack - minAttack)) * (minParticleMaskValue - maxParticleMaskValue) + maxParticleMaskValue;
Your answer
Follow this Question
Related Questions
Matrix Multiplication, Standard Deviant and More usefull methods 2 Answers
Minimap rotating nodes 1 Answer
Extending a vector? 4 Answers
Convert from m/s to m/fixedFrame 1 Answer
faulty randomness (uniform RNG) 0 Answers