Is it possible to change post processing from a script?

38 Views Asked by At

I’m making a 2D horror game for a jam and the idea came up that in some scary moments the chromatic effect will increase along the edges of the screen, but I don’t know how to do it. Can you give an example of a script that should be used for this? So that when entering a 2D collider, the chromatic effect will gradually increase and upon leaving it gradually fell

I'm a complete newbie to Unity, so I tried to find a solution on YouTube or other sites, but I couldn't find it._.

here is an example of a script that I found, but I don’t understand how their post-processing can take the chromatic effect and indicate it in the script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class ChromaticEffect : MonoBehaviour
{
    public Collider2D triggerCollider;
    public bool isTriggered;
    public PostProcessVolume postProcessVolume;
    public ChromaticAberration chromaticAberration;
    public float maxChromaticIntensity;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision == triggerCollider)
        {
            isTriggered = true;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision == triggerCollider)
        {
            isTriggered = false;
        }
    }

    private void Update()
    {
        if (isTriggered)
        {
            chromaticAberration.intensity.value = 
            Mathf.Lerp(chromaticAberration.intensity.value, 
            maxChromaticIntensity, Time.deltaTime);
        }
        else
        {
            chromaticAberration.intensity.value = Mathf.Lerp(chromaticAberration.intensity.value, 0f, Time.deltaTime);
        }
    }
}
1

There are 1 best solutions below

0
On

Afaik you would access it via e.g.

chromaticAberration = postProcessVolume.profile.GetSetting<ChromaticAberration>();

or if you are not sure the volume even has this setting you can also use

if(!postProcessVolume.profile.TryGetSetting(out chromaticAberration))
{
    chromaticAberration = postProcessVolume.profile.AddSetting<ChromaticAberration>();
}

See


Then further currently your fade has an issue: Mathf.Lerp is a linear interpolation between the first and second value using a factor between 0 (fully first value) and 1 (fully second value).

You are always passing in Time.deltaTime which is very small (e.g. 60 frames per second => 0.017) => This will barely change the value.

If you rather wanted a linear fade effect you should rather e.g. use Mathf.MoveTowards

chromaticAberration.intensity.value = Mathf.MoveTowards(chromaticAberration.intensity.value, 0f, Time.deltaTime);

this means it will move the value towards 0 with a constant "speed" of 1 unit per second.

You could actually fit the entire Update in a single line using a ternary:

chromaticAberration.intensity.value = Mathf.MoveTowards(
    chromaticAberration.intensity.value, 
    isTriggered ? maxChromaticIntensity : 0f, 
    Time.deltaTime);