- Home /
How can i change post processing at run time using if statements
basically im creating a bullet time game where time slows down if the player is aiming. I also want the vignette intensity to increase from the value it is to the higher value i want it to be and then when the player stops aiming to change from the higher back to the lower. I also want this effect to be smooth, so probably using Mathf.lerp. Ive followed a few tutorials on how to change postprocessing at run time and can do that but somehow when i use if statemtents it messes everything up.
Answer by Pokedlg3 · Feb 10, 2021 at 02:06 AM
You would have to get the Post Process Volume and try to take the vignette and put it as an output for the Vignette variable and then be able to change its properties there. Don't forget to put the UnityEngine.Rendering.PostProcessing library.
Something like this would work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
public class YourScript : MonoBehaviour
{
[Range(0,1)]
public float lerpTime;
Vignette vignette = null;
float max = 1f;
float min = 0f;
bool aiming;
void Start()
{
PostProcessVolume volume = GetComponent<PostProcessVolume>();
volume.profile.TryGetSettings(out vignette);
}
void Update()
{
if(vignette !=null && aiming == true){
vignette.intensity.value = Mathf.Lerp(vignette.intensity.value, max,Time.deltaTime * lerpTime);
}
else if(vignette !=null && aiming == false)
{
vignette.intensity.value = Mathf.Lerp(vignette.intensity.value, min,Time.deltaTime * lerpTime);
}
}
}
When running it may generate a Null Reference error, but don't worry, it will work.
Thank you!!! This worked very well with very little tweaking needed. Only thing i really NEEDED to change was to used unscaledDeltaTime as through my slow motion function I changed deltaTime . Here is my updated version of the script for anybody who stumbles across this wanting to accomplish the exact same thing. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering.PostProcessing; public class Slow$$anonymous$$oPostProcess : $$anonymous$$onoBehaviour { public PostProcessVolume volume; private Vignette vignette = null; private float intensity; public float lerpTime; float max = .6f; float $$anonymous$$ = .4f; bool ai$$anonymous$$g = false; void Start() { volume.profile.TryGetSettings(out vignette); } void Update() { if (Input.Get$$anonymous$$ouseButtonDown(1)) { ai$$anonymous$$g = true; }else if (Input.Get$$anonymous$$ouseButtonUp(1)) { ai$$anonymous$$g = false; } if (vignette != null && ai$$anonymous$$g == true) { vignette.intensity.value = $$anonymous$$athf.Lerp(vignette.intensity.value, max, Time.unscaledDeltaTime * lerpTime); } else if (vignette != null && ai$$anonymous$$g == false) { vignette.intensity.value = $$anonymous$$athf.Lerp(vignette.intensity.value, $$anonymous$$, Time.unscaledDeltaTime * lerpTime); } } }
Answer by ThiccSnowM4N · Feb 10, 2021 at 06:01 PM
for whatever reason my code would not format correctly in my response to the above person so here it is. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering.PostProcessing;
public class SlowMoPostProcess : MonoBehaviour
{
public PostProcessVolume volume;
private Vignette vignette = null;
private float intensity;
public float lerpTime;
float max = .6f;
float min = .4f;
bool aiming = false;
void Start()
{
volume.profile.TryGetSettings(out vignette);
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
aiming = true;
}else if (Input.GetMouseButtonUp(1))
{
aiming = false;
}
if (vignette != null && aiming == true)
{
vignette.intensity.value = Mathf.Lerp(vignette.intensity.value, max, Time.unscaledDeltaTime * lerpTime);
}
else if (vignette != null && aiming == false)
{
vignette.intensity.value = Mathf.Lerp(vignette.intensity.value, min, Time.unscaledDeltaTime * lerpTime);
}
}
}