- Home /
Keeping track of object that is set via "if..Raycast Statement
Hello everyone,
I have a top-down 3d Scene and I am fading out the alpha of a roof when i enter the Building. Here is the code: #pragma strict var dir: Vector3; var Obj: RaycastHit;
function Start () {
dir = transform.TransformDirection (Vector3.up);
}
function Update () {
var hit : RaycastHit;
if(Physics.Raycast (transform.position, dir, hit, 20)){
var Obj = hit.transform;
Obj.renderer.material.color.a -= renderer.material.color.a * Time.deltaTime;
}
}
It works but the problem is i need to make the roof (Alpha component) visible again after i leave, but i only get the name of the object when the Raycast hits it - because after i leave the raycast gives out Null.
How could i solve this? How can i know via script which objects Alpha component i need to set back to 1.
Thanks for the help in advance. -Alexander
You can simply make Obj a class instance variable. Declare it at the top of the file, then you can bring the alpha back up if the Raycast() fails. But this solution has a flaw. If you have more than one object that can fade, it is possible to exit one and entera new one before the first has completely faded up. If this is an issue, I'd move the fading code to the individual objects and just have the Raycast() code tell the objects to fade up or down. That way if you move on to a new object, the original object can keep fading up until alpha is 1.0.
Thanks. I was thinking about handling this in a script on each individual Object but i was hoping there was a easier way. I guess i will have to do it that way then. Thanks for the answer.
There is almost always more solutions. For example, you could get around the problem I outlined in the previous comment by immediately bringing the alpha to 1.0 on the previous object when a new fading object is found. Or you could use a Queue to manage a list of fading objects, but I think the right thing is to put the fading code in the individual objects.