Hiya folks!
So here’s the deal, I’m trying to map somewhat disparate values together to be controlled by one slider.
Through the expert advice I’ve gotten on here I’ve been able to map the playback speed of an animation based on a slider (thank you Unity community!). Now I’m trying to map the same values from that slider to a particle emitter (Rate Over Time). Slider goes from .3 to .9 and I’d like the particles to read that range but go from 10 to 100 instead. I’ve left the other tries I made commented out so you can see the bad ideas I’ve had so far. D’oh!
I’m sure I’ve made all manner of syntax and structure errors here…
Any ideas?
Cheers
Jeff
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FoggingAmount : MonoBehaviour
{
ParticleSystem myParticleSystem;
ParticleSystem.EmissionModule emissionModule;
public Slider slider;
void Start()
{
myParticleSystem = GetComponent<ParticleSystem>();
emissionModule = myParticleSystem.emission;
}
void Update()
{
emissionModule.rateOverTime = EmitRate();
}
private float Emitrate();
float fromslider = slider.value;
int value = .5f;
int newValue = Mathf.Lerp(10.0f, 100.0f, Mathf.InverseLerp(0.75f, 0.9f, fromslider));
return (10.0f - 100.0f / ((fromslider - .75f) / (0.9f - 0.3f));
///return int newValue;
/*private float emitRate()
{
float fromslider = slider.value;
float Remap = (float .75f, float .9f, float 0.01f, float 100f, fromslider)
float t = InvLerp(.75f, .9f, fromslider);
return Lerp(0.01f, 100f, t);
}*/
/*float fromslider = slider.value;
float speedmod = Mathf.InverseLerp(10f, 100f, fromslider);
return (fromslider - .01f) / (100f - 10f);*/
}
I would create a public AnimationCurve object:
public AnimationCurve SliderToParticles;
Then put in two control points, one at (0.3, 10) and one at (0.9, 100) in the editor.
Finally use SliderToParticles.Evaluate(); to produce the desired particle rate based on the slider.
Ok, fixed the more egregious errors so it’ll compile, but it doesn’t affect the flow rate…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FoggingAmount : MonoBehaviour
{
ParticleSystem myParticleSystem;
ParticleSystem.EmissionModule emissionModule;
public Slider slider;
void Start()
{
myParticleSystem = GetComponent<ParticleSystem>();
emissionModule = myParticleSystem.emission;
}
void Update()
{
emissionModule.rateOverTime = Emitrate();
}
private float Emitrate()
{
float fromslider = slider.value;
float value = .5f;
int newValue = (int)Mathf.Lerp(10.0f, 100.0f, Mathf.InverseLerp(0.75f, 0.9f, fromslider));
return (10.0f - 100.0f / ((fromslider - .75f) / (0.9f - 0.3f)));
}
///return int newValue;
/*private float emitRate()
{
float fromslider = slider.value;
float Remap = (float .75f, float .9f, float 0.01f, float 100f, fromslider)
float t = InvLerp(.75f, .9f, fromslider);
return Lerp(0.01f, 100f, t);
}*/
/*float fromslider = slider.value;
float speedmod = Mathf.InverseLerp(10f, 100f, fromslider);
return (fromslider - .01f) / (100f - 10f);*/
}
I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run?
what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.
Also try hard-wiring some values to return from EmitRate, see if the particles are behaving.
Kurt-Dekker:
I would create a public AnimationCurve object:
public AnimationCurve SliderToParticles;
Then put in two control points, one at (0.3, 10) and one at (0.9, 100) in the editor.
Finally use SliderToParticles.Evaluate(); to produce the desired particle rate based on the slider.
I’ve never heard of an AnimationCurve object! What would that be?
Kurt-Dekker:
I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run?
what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.
Also try hard-wiring some values to return from EmitRate, see if the particles are behaving.
Yeah , when I dump all the lerping the slider does control the rate of particles. It’s just the fancy lerping that’s the buggy bit.
MrBlack3d:
What would that be?
Try it, you’ll like it, but still, do the basic steps of debugging above too.
ok. this works but the numbers being returned are bonkers! Instead of 10 to 100 I’m getting 480 to 975!
Clearly I’m not a math guy…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FoggingAmount : MonoBehaviour
{
ParticleSystem myParticleSystem;
ParticleSystem.EmissionModule emissionModule;
public Slider slider;
void Start()
{
myParticleSystem = GetComponent<ParticleSystem>();
emissionModule = myParticleSystem.emission;
}
void Update()
{
emissionModule.rateOverTime = Emitrate();
}
private float Emitrate()
{
float fromslider = slider.value;
int newValue = (int)Mathf.Lerp(10.0f, 100.0f, Mathf.InverseLerp(0.75f, 0.9f, fromslider));
return (10.0f - 100.0f / ((fromslider - .75f) / (0.9f - 0.3f)));
}
///return int newValue;
/*private float emitRate()
{
float fromslider = slider.value;
float Remap = (float .75f, float .9f, float 0.01f, float 100f, fromslider)
float t = InvLerp(.75f, .9f, fromslider);
return Lerp(0.01f, 100f, t);
}*/
/*float fromslider = slider.value;
float speedmod = Mathf.InverseLerp(10f, 100f, fromslider);
return (fromslider - .01f) / (100f - 10f);*/
}
I dunno what line 30 is. Why don’t you just return newValue? The inverse lerp and lerp look reasonable…