Fade object close to camera

Could anybody help me with a camera script that fades out object when they are close to the camera.

If the object is close/in front of the camera the opacity is set to 50% …

Any hints/help ???

Use GameObject.renderer.material.color.a to control the alpha of an object.

ok, Nice it worked out after a bit of truble …
I realized that I had to use the transparent shader (But I want to use the Toon shader)

How could I make a Toon shaded object transparent ?

I used this script

if (Camera.main.WorldToViewportPoint(transform.position).z < 5)
		renderer.material.color.a = 0.5;

Instead of

if (Camera.main.WorldToViewportPoint(transform.position).z < 5)

use

(Camera.main.transform.position - transform.position).magnitude

It tells you how far away something is.

Toon shaders are just like diffuse shaders, and also can use renderer.material.color.a.

ok, thanks.

But the renderer.material.color.a didn’t work with toon shaders ( I have to use the transparent shaders )

Hmm…I dont’ actually know if there exists a transparent toon shader.

I did not write this. So I give credit to the person who did.

204309–7528–$fade_object_between_camera_and_player_902.js (4.39 KB)

That script seems like just what I need, but I get an error on line 125 stating that there is already a local variable with the name ‘newMaterial’.

Why not do this in a shader? Arguably a lot more optimized and the standard way of doing this.
Seeing the camera is already being parsed to the shader, and ofcourse the vertex positions, it’s should be no issue to calculate the distance and go from there.