- Home /
[well commented code example] problem animating color.alpha of objects above the character
@@^&#*$&(()!! All day work, I can't figure out what the hell...
We have a character. and there will be objects above it. All I want is when objects are above the character, their Color.a goes down. (up to, say .2, and, besides, if they don't have Transparent/Diffuse shader yet, they are given one) and when those objects are no longer above the character their color.a goes up to 1.
And I can't figure that out!!!
Here's my code, which I added to the character:
//Timer: so that the rays aren't cast every frame, but - for example- every half a second would suffice
//trying to reduce performance costs this way
var checkDelay = 0.5;
private var nextCheck = 0.5;
function Timer(){
if(Time.time >= nextCheck)
{
nextCheck = Time.time + checkDelay;
return true;
}
else
return false;
}
var selected = new ArrayList(); //will store renderers which are selected this frame
var prevSel = new ArrayList(); //will store renderers which were selected last frame
var toFadeIn= new ArrayList(); //will store renderers which need to be faded back into full visibility
function Update () {
if(Timer()){
AllR3();
}
FadeIn(toFadeIn);
FadeOut(selected);
}
function AllR3(){
var hits:RaycastHit[];
hits = Physics.RaycastAll(transform.position,Vector3.up);
//deals with what happened last frame
//renderers hit on the last frame will be saved in prevSel (previous selection)
//and "selected" cleared to
prevSel = selected;
selected.Clear();
//collects all the renderers in a list
//and changes shaders to transparent when necessary
for(var i=0; i<hits.Length; i++){
var rend = hits[i].collider.renderer;
if(rend){
rend.material.shader = Shader.Find("Transparent/Diffuse");
selected.Add(rend);
}
}
//using renderers selected on previous frame (prevSel)
//and those just selected,
//to find those rendenderes who were selected last frame, but arent any more...
for( i =0; i<prevSel.Count;i++){
if(selected.Contains(prevSel[i])){
prevSel.RemoveAt(i);
i--;
}
else
toFadeIn.Add(prevSel[i]);
}
//..and makes a "toFadeIn" array that holds all of them
}
//functions that do the Color.a animation - fading in and fading out
var targetFadeOut = .3; //how much transparent we want the renderes to get when character is under them
var fadeDuration= .3; //how much time it will take to change the color.a
function FadeIn(obj){
if(obj.Count ==0) //if there aren't any elements to animate - don't continue with the script
return;
for(var i = 0; i< obj.Count; i++){
if(obj[i].material.color.a == 1.0){
//finish
//~ obj[i].material.shader = Shader.Find("Diffuse"); //alternatively - go back to diffuse
obj = obj.RemoveAt(i); //the only way to get out of "toFadeIn" array is to actually get color.a to 1.0, this will remove the renderer from array as there's nothing else we want to do with that renderer
i--;//fix cause removing an element will put the next element in its place, which still needs to be analyzed
}
else
obj[i].material.color.a = Mathf.Lerp(obj[i].material.color.a,1.0,fadeDuration * Time.deltaTime); //if target is not achieved - animate towards it
}
}
function FadeOut(obj){
if(obj.Count == 0)
return;
for(var i=0; i< obj.Count; i++)
if(obj[i].material.color.a == targetFadeOut){
//finish
//still will need till it's no longer selected, moves to toFadeIn, fades in and only then disappears from our arrays
}
else{
obj[i].material.color.a = Mathf.Lerp(obj[i].material.color.a,targetFadeOut, fadeDuration * Time.deltaTime);
print("fade out inside else");
}
}
Could you give me a hand?
Comment
Your answer
Follow this Question
Related Questions
How to find out if raycast dont hit anything 1 Answer
Help - Selection System 1 Answer
Change Color of Object when NOT looking at it 1 Answer
Referencing color change when object is 'deselected' 2 Answers
Get pixel from Rotated Texture2D 1 Answer