Camera Blur Lerp Help C#

Hello Everyone!

I’m attaching the Blur(JS) script to my Main Camera and I was trying to get the “background” to blur when I hover over something. I kind of have it working but I have 2 issues:

  1. How can I have things on another layer that aren’t getting blurred by the main camera
  2. I can’t get it to lerp smoothly between values.

Here is what I have so far.

using UnityEngine;
using System.Collections;

public class BlurBGWhenOver : MonoBehaviour {

	Blur cameraBlur;
	// Use this for initialization
	void Start () {
		cameraBlur = Camera.main.GetComponent<Blur>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnMouseEnter(){
		cameraBlur.enabled = true;
		cameraBlur.blurSize = Mathf.Lerp (cameraBlur.blurSize, 4, Time.deltaTime * 2);
	}
	void OnMouseExit(){
		cameraBlur.enabled = false;
		cameraBlur.blurSize = Mathf.Lerp (cameraBlur.blurSize, 0, Time.deltaTime * 2);
	}
}

So again, why is this not lerping smoothly between values. How can I put the quad on another layer so that when the main camera blurs the “background” the things I want to remain in the foreground don’t get blurred. Thanks so much!

Cause OnMouseEnter and OnMouseExit aren’t looping

using UnityEngine;

using System.Collections;

 

public class BlurBGWhenOver : MonoBehaviour {

 

    Blur cameraBlur;
    Bool isHover;

    // Use this for initialization

    void Start () {

        cameraBlur = Camera.main.GetComponent<Blur>();

    }

    

    // Update is called once per frame

    void Update () {
     

    if(isHover)
{
 cameraBlur.enabled = true;

   cameraBlur.blurSize = Mathf.Lerp (cameraBlur.blurSize, 4, Time.deltaTime * 2);

}

else

{
 cameraBlur.enabled = false;

        cameraBlur.blurSize = Mathf.Lerp (cameraBlur.blurSize, 0, Time.deltaTime * 2);
}
    

    }

    

    void OnMouseEnter(){

       isHover = true;
    }

    void OnMouseExit(){

       isHover = false;

    }

}

Awesome! You’re totally right. Although, when it unfades the lerping does not work because it disables the script before the lerp can happen. How could I add some kind of delay or instruction that says wait for the lerping to finish? Would I need to make this a coroutine?

else
{

        cameraBlur.blurSize = Mathf.Lerp (cameraBlur.blurSize, 0, Time.deltaTime * 2);
        if (cameraBlur.blurSize < 0.1f)
            cameraBlur.enabled = false;

}