Search Unity

Problems with Lerping ChromaticAbarration...

Discussion in 'Scripting' started by MartinBuriak, Mar 21, 2019.

  1. MartinBuriak

    MartinBuriak

    Joined:
    Jan 12, 2019
    Posts:
    2
    [SOLVED]

    Hello guys, I have a project where I want to increase chromatic aberration from code when an object hits another object (chromatic aberration is a part of the post processing module/profile)... at this point everything is running good, here is the code for that part...

    Code (CSharp):
    1.  
    2.     public PostProcessingProfile profile;
    3.  
    4.     private void OnTriggerEnter(Collider other)
    5.     {
    6.         ChromaticAberrationIncrease();
    7.     }
    8.  
    9.     public void ChromaticAberrationIncrease()
    10.     {
    11.         ChromaticAberrationModel.Settings chromaticAberrationSettings = profile.chromaticAberration.settings;
    12.         chromaticAberrationSettings.intensity = chromaticAberrationSettings.intensity + 0.1f;
    13.         profile.chromaticAberration.settings = chromaticAberrationSettings;
    14.     }
    But I want it to change it's value smoothly, I tried to do it and there were no errors but it didn't work, it just changed the chromatic aberration as it does in the previous part (not smoothly),
    here's the code:

    Code (CSharp):
    1.  
    2.     public PostProcessingProfile profile;
    3.    
    4.     private void OnTriggerEnter(Collider other)
    5.     {
    6.         ChromaticAberrationIncrease();
    7.     }
    8.  
    9.     public void ChromaticAberrationIncrease()
    10.     {
    11.         ChromaticAberrationModel.Settings chromaticAberrationSettings = profile.chromaticAberration.settings;
    12.         chromaticAberrationSettings.intensity = Mathf.Lerp(chromaticAberrationSettings.intensity, 1f, 0.5 * Time.deltaTime);
    13.         profile.chromaticAberration.settings = chromaticAberrationSettings;
    I used the Lerp() method for this one and I think I know what's the problem but I don't know how to solve it, if you remove the line that returns the chromatic aberration intensity value in to the inspector in the first code: (profile.chromaticAberration.settings = chromaticAberrationSettings; ), you can see that the value of the chromatic aberration isn't changing at all, well, because it's never updating the inspector...
    So I think that the problem is because while the chromatic aberration lerps in the second code, the inspector isnt updating, and the inspector gets the value only when the Lerp method is done... it gets only the start value (0) and the end value (1) and just jumps instantly between them...
    Please guys, help me on this problem.
     
    Last edited: Mar 21, 2019
  2. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    Apparently ChromaticAberrationIncrease() is only called once per collision event, right? If you want smooth transitions, you need to call it repeatedly, gradually increasing the interpolation value each time.
     
    MartinBuriak likes this.
  3. MartinBuriak

    MartinBuriak

    Joined:
    Jan 12, 2019
    Posts:
    2
    Hmm okay, ive tried using OnCollisionStay instead of OnCollisionEnter and it worked! thank you for your help!!!