- Home /
Having a door dissapear once a key is collected
I have a door object in my maze, that has a mesh collider on it so you cant pass through it from the beginning, but i want to make it so that once you activate the trigger animation set up on a chest you will be able to get through the door once this event has happened. This is the code i have for the chest,
var displayMessage = false;
function OnTriggerEnter (Player : Collider) { animation.Play("Take 001"); displayMessage = true; yield WaitForSeconds(3); displayMessage = false; }
function OnGUI ( ) { if ( displayMessage ) { GUI.Label(new Rect(Screen.width /2.5, Screen.height / 2, 200, 200), "You received rusty key"); } }
Once that has happened i want it so that when you approach the door you can now get through it, also that this would be the end of the game so id want it to stop after you got through the door.
Thanks in advance for any help
Answer by Kourosh · Apr 15, 2011 at 01:38 AM
You need to disable the collider attached to the door object. I don't what kinda door is that that you can walk through it tho lol. Anyway you can simply remove the collider:
Destroy(GetComponent(MeshCollider));
And use box collider cause mesh collider is more costly on the performance and it's not really necessary for an object like a door. It'll look something like this:
function OnTriggerEnter (Player : Collider)
{
var door:GameObject = GameObject.Find("door");
Destroy(door.GetComponent(MeshCollider));
animation.Play("Take 001");
displayMessage = true;
yield WaitForSeconds(3);
displayMessage = false;
FadeOut.fadeOut = true; //---> change the fadeOut var in FadeOut.js to start fading out.
}
About the game end. You should know what happens when your game is over. For turning the screen to black. You have two options:
put a black plain in front of a camera and make an alpha animation or simply use Lerp to turn the alpha from 0 to 1 over time.
Attach this script to the black plain and name the script "FadeOut.js":
var increment:float; static var fadeOut:boolean = false;
function Update(){ if(fadeOut){ renderer.material.color.a = Mathf.Lerp(0,1,increment); increment +=0.02; } }
Or you might wanna try iTween plugin. It's free and does that for you in only one line code.
yeah i no lol, ive ran out of time to have it animate opening unfortunately bt this is the last step i need and i can be half happy then. ok thanks for this sorry to be a pain but what would that look like in code with reference to what i want. my door object is called "door", i dont understand the get component bit sorry, totally new to this.
sorry lol as in how do i make it only disable it after the chest animation has been triggered?
no no, sorry for confusing you. Just change your current script (chest) no need to attack anything to your door.
ok yeah i just realised it was the same $$anonymous$$us the get component addition i have it working thankyou, that was my misunderstanding. would u be able to explain that setting up an ending at all? i tried to download itween bt i cant read the file for some reason so gonna have to go with the other method. unfortunately i dont no wat u mean by the lerp to turn alpha from 0 to 1 sorry