Character from transparent to opaque

When the boy close to those ball, the boy will become translucent.
Thus these ball will not block by boy’s body.
Player (we) will see boy eat those ball.

My idea is:
Create a big cube to cover with those ball .
And let big cube don’t show on scene.
As long as people within big cube, the boy will become translucent.
But the boy is out of big cube, the boy will restore to original appearance (opaque).

What can I do this code?
Thanks everybody :slight_smile:

Sounds like you’ve got the right idea.

The easiest way I can think of at the moment to actually implement the transparency would be as follows:

  • Use (or create) an alpha-blended shader with a ‘color’ parameter for the character’s material(s).

  • Set the alpha values for the materials to 1 when the character exits the trigger, and to some value < 1 when the character enters the trigger.

Note that you’ll probably want to access the material via renderer.material; that way, a copy will be made that you can freely modify. (Note that if you don’t clean up this duplicated material manually, it won’t be cleaned up until the scene unloads, but it doesn’t seem like that’d be a problem here.)

It might also be worthwhile to switch to a non-alpha-blended material once the alpha value has been reset to 1, just to avoid unnecessary blending operations.

Also, you could of course interpolate/animate the alpha value over time for a smoother transition.

Anyway, that’s what first comes to mind. Maybe someone else will have a better suggestion though.

When posting to both the forums and Unity Answers, please provide a cross-link to your other thread. Otherwise, people tend to end up duplicating their efforts (as is the case here).

Thanks Jesse Anders and everybody!!! I succeed!!!

But I have a warning:

ArgumentException: Find can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. collision_cube…ctor () (at Assets/Standard Assets/Scripts/collision_cube.js:1)

How can I solve this problem?


First, I choose “shadow material” to big cube’s materials.
Let it becomes transparent.
In this way, cube doesn’t show.
Tag name is called “glass”.

Second, put this script into boy:
(Boy’s model is compose by 5 polygon)

var shader1 : Shader = Shader.Find( "Diffuse" );
var shader2 : Shader = Shader.Find( "FX/Glass/Stained BumpDistort" );
var face : GameObject;
var face2 : GameObject;
var face3 : GameObject;
var face4 : GameObject;
var face5 : GameObject;


function OnTriggerStay(other : Collider){
if (other.gameObject.tag == "glass" )  {
face.renderer.material.shader = shader2;
face2.renderer.material.shader = shader2;
face3.renderer.material.shader = shader2;
face4.renderer.material.shader = shader2;
face5.renderer.material.shader = shader2;
}
}

function OnTriggerExit(other : Collider){
if (other.gameObject.tag == "glass" )  {
face.renderer.material.shader = shader1;
face2.renderer.material.shader = shader1;
face3.renderer.material.shader = shader1;
face4.renderer.material.shader = shader1;
face5.renderer.material.shader = shader1;
}
}

The error message actually (and quite helpfully) provides an answer to this question:

Maybe you could try animating the material’s alpha value. From the Unity Scripting Reference manual I pulled this code. It animates the alpha based on the texture’s horizontal offset, but maybe you could take the same concept and apply it to the distance to the boy character.

// Animate color's alpha and main texture's horizontal offset.

function Start () {
    var clip = new AnimationClip ();
    clip.SetCurve ("", Material, "_Color.a",
        AnimationCurve (Keyframe(0, 0, 0, 0), Keyframe(1, 1, 0, 0)));
    clip.SetCurve ("", Material, "_MainTex.offset.x",
        AnimationCurve.Linear(0, 1, 2, 3));
    animation.AddClip (clip, clip.name);
    animation.Play(clip.name);
}
@script RequireComponent(Animation)

That’s what I suggested in the first reply to the thread. (It was also suggested in the corresponding UA thread.)

Alpha Blended is shader in "Mobile/Particles/Alpha Blended”?
But I don’t understand to use ><
And my character hasn’t action now (no animation).

Is write like this?
(Model is compose by 5 polygon)

function OnTriggerStay(other : Collider){
if (other.gameObject.tag == "glass" )  {
face.renderer.material.color.a = 0.5;
face2.renderer.material.color.a = 0.5;
face3.renderer.material.color.a = 0.5;
face4.renderer.material.color.a = 0.5;
face5.renderer.material.color.a = 0.5;
}
}

function OnTriggerExit(other : Collider){
if (other.gameObject.tag == "glass" )  {
face.renderer.material.color.a = 1;
face2.renderer.material.color.a = 1;
face3.renderer.material.color.a = 1;
face4.renderer.material.color.a = 1;
face5.renderer.material.color.a = 1;
}
}

Thanks Jesse Anders and bugzilla :slight_smile: