Search Unity

remapping value issues (for me anyway)

Discussion in 'Scripting' started by MrBlack3d, Nov 7, 2020.

  1. MrBlack3d

    MrBlack3d

    Joined:
    Feb 5, 2019
    Posts:
    33
    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
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class FoggingAmount : MonoBehaviour
    7. {
    8.     ParticleSystem myParticleSystem;
    9.     ParticleSystem.EmissionModule emissionModule;
    10.     public Slider slider;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         myParticleSystem = GetComponent<ParticleSystem>();
    16.         emissionModule = myParticleSystem.emission;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         emissionModule.rateOverTime = EmitRate();
    22.  
    23.     }
    24.  
    25.     private float Emitrate();
    26.         float fromslider = slider.value;
    27.         int value = .5f;
    28.         int newValue = Mathf.Lerp(10.0f, 100.0f, Mathf.InverseLerp(0.75f, 0.9f, fromslider));
    29.         return (10.0f - 100.0f / ((fromslider - .75f) / (0.9f - 0.3f));
    30.  
    31.  
    32.  
    33.         ///return int newValue;
    34.  
    35.  
    36.     /*private float emitRate()
    37.     {
    38.  
    39.         float fromslider = slider.value;
    40.         float Remap = (float .75f, float .9f, float 0.01f, float 100f, fromslider)
    41.         float t = InvLerp(.75f, .9f, fromslider);
    42.         return Lerp(0.01f, 100f, t);
    43.     }*/
    44.  
    45.  
    46.  
    47.  
    48.     /*float fromslider = slider.value;
    49.    float speedmod = Mathf.InverseLerp(10f, 100f, fromslider);
    50.    return (fromslider - .01f) / (100f - 10f);*/
    51.  
    52.  
    53. }
    54.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I would create a public AnimationCurve object:

    Code (csharp):
    1. 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.
     
  3. MrBlack3d

    MrBlack3d

    Joined:
    Feb 5, 2019
    Posts:
    33
    Ok, fixed the more egregious errors so it'll compile, but it doesn't affect the flow rate..
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class FoggingAmount : MonoBehaviour
    7. {
    8.     ParticleSystem myParticleSystem;
    9.     ParticleSystem.EmissionModule emissionModule;
    10.     public Slider slider;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         myParticleSystem = GetComponent<ParticleSystem>();
    16.         emissionModule = myParticleSystem.emission;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         emissionModule.rateOverTime = Emitrate();
    22.  
    23.     }
    24.  
    25.     private float Emitrate()
    26.         {
    27.         float fromslider = slider.value;
    28.         float value = .5f;
    29.         int newValue = (int)Mathf.Lerp(10.0f, 100.0f, Mathf.InverseLerp(0.75f, 0.9f, fromslider));
    30.         return (10.0f - 100.0f / ((fromslider - .75f) / (0.9f - 0.3f)));
    31.         }
    32.  
    33.  
    34.  
    35.         ///return int newValue;
    36.  
    37.  
    38.     /*private float emitRate()
    39.     {
    40.  
    41.         float fromslider = slider.value;
    42.         float Remap = (float .75f, float .9f, float 0.01f, float 100f, fromslider)
    43.         float t = InvLerp(.75f, .9f, fromslider);
    44.         return Lerp(0.01f, 100f, t);
    45.     }*/
    46.  
    47.  
    48.  
    49.  
    50.     /*float fromslider = slider.value;
    51.    float speedmod = Mathf.InverseLerp(10f, 100f, fromslider);
    52.    return (fromslider - .01f) / (100f - 10f);*/
    53.  
    54.  
    55. }
    56.  
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    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.
     
  5. MrBlack3d

    MrBlack3d

    Joined:
    Feb 5, 2019
    Posts:
    33
    I've never heard of an AnimationCurve object! What would that be?
     
  6. MrBlack3d

    MrBlack3d

    Joined:
    Feb 5, 2019
    Posts:
    33
    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.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Try it, you'll like it, but still, do the basic steps of debugging above too.
     
  8. MrBlack3d

    MrBlack3d

    Joined:
    Feb 5, 2019
    Posts:
    33
    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.....
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class FoggingAmount : MonoBehaviour
    7. {
    8.     ParticleSystem myParticleSystem;
    9.     ParticleSystem.EmissionModule emissionModule;
    10.     public Slider slider;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         myParticleSystem = GetComponent<ParticleSystem>();
    16.         emissionModule = myParticleSystem.emission;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         emissionModule.rateOverTime = Emitrate();
    22.  
    23.     }
    24.  
    25.     private float Emitrate()
    26.         {
    27.  
    28.         float fromslider = slider.value;
    29.         int newValue = (int)Mathf.Lerp(10.0f, 100.0f, Mathf.InverseLerp(0.75f, 0.9f, fromslider));
    30.         return (10.0f - 100.0f / ((fromslider - .75f) / (0.9f - 0.3f)));
    31.  
    32.         }
    33.  
    34.  
    35.  
    36.         ///return int newValue;
    37.  
    38.  
    39.     /*private float emitRate()
    40.     {
    41.  
    42.         float fromslider = slider.value;
    43.         float Remap = (float .75f, float .9f, float 0.01f, float 100f, fromslider)
    44.         float t = InvLerp(.75f, .9f, fromslider);
    45.         return Lerp(0.01f, 100f, t);
    46.     }*/
    47.  
    48.  
    49.  
    50.  
    51.     /*float fromslider = slider.value;
    52.    float speedmod = Mathf.InverseLerp(10f, 100f, fromslider);
    53.    return (fromslider - .01f) / (100f - 10f);*/
    54.  
    55.  
    56. }
    57.  
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I dunno what line 30 is. Why don't you just return newValue? The inverse lerp and lerp look reasonable...