How to change the intensity of Chromatic Aberration from Post Processing by script?
using UnityEngine;
using System.Collections;
using UnityEngine.PostProcessing;
[RequireComponent(typeof(PostProcessingBehaviour))]
public class CarCamera : MonoBehaviour{
private Rigidbody playerRigid;
public Transform playerCar;
private PostProcessingProfile PPProfile;
private ChromaticAberrationModel CAberration;
//min and max values for intensity
private float minimumChromaticAb =0F;
private float maximumChromaticAb =1F;
void Start(){
playerCar = GameObject.FindObjectOfType<CarBehaviour>().transform;
playerRigid = playerCar.GetComponent<Rigidbody>();
CAberration = PPProfile.chromaticAberration;
var behaviour = GetComponent<PostProcessingBehaviour>();
behaviour.profile = PPProfile;
}
void Update(){
//Changes the intensity of the Chromatic Aberration according to the speed of the car
CAberration.settings.intensity = Mathf.Lerp (minimumChromaticAb, maximumChromaticAb, (playerRigid.velocity.magnitude * 3f) / 150f);
PPProfile.chromaticAberration = CAberration;
}
}
Unity shows this error: Assets/Scripts/CarCamera.cs(63,16): error CS1612: Cannot modify a value type return value of `UnityEngine.PostProcessing.ChromaticAberrationModel.settings'. Consider storing the value in a temporary variable
I've tried everything. But I can not solve this miserable mistake.
Answer by oriont · Oct 24, 2017 at 01:18 AM
The best way I found to do it is to create an instance of the settings of the chromatic aberration and change that. Then you set the settings from the real profile to your clone of it.
var ca = PPProfile.chromaticAberration.settings;
ca.intensity = Mathf.Lerp (minimumChromaticAb, maximumChromaticAb, (playerRigid.velocity.magnitude * 3f) / 150f);
PPProfile.chromaticAberration.settings = ca;
I haven't tested this exact code, but I hope this helps!
Your answer
Follow this Question
Related Questions
Visual Studio not working correctly with Unity 1 Answer
Kart Skidding UI 0 Answers
Production Code from external Plugins and Extension 0 Answers
Unity is currently almost unusable 1 Answer
How can I create a new game based off of an existing one? 2 Answers