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);
}
}
}
Afaik you would access it via e.g.
or if you are not sure the volume even has this setting you can also use
See
PostProcessVolume.profile
PostProcessProfile.GetSetting<T>
PostProcessProfile.TryGetSetting<T>
PostProcessProfile.AddSetting<T>
Then further currently your fade has an issue:
Mathf.Lerp
is a linear interpolation between the first and second value using a factor between0
(fully first value) and1
(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
this means it will move the value towards
0
with a constant "speed" of1 unit per second
.You could actually fit the entire
Update
in a single line using a ternary: