- Home /
How to trigger a door with an object not first person controller
Hello! I am very new to Unity and scripting, but I'm trying to make a game where the player finds an object and picks it up. The player then goes to a door (carrying the object) and the object triggers the door to open to finish the level. My problem is that I cannot figure out how to get the object to trigger the door open. I tried several different scripts, but only one of them worked and I'm too new to be able to fix the other ones. The one that worked is perfect, except that not only the object triggers the door but the first person controller, which means that at any point the player can walk up to the door and win the game, which defeats the purpose of playing it. Can anyone help me figure out a way to solve this problem?
Thanks!
Have you tried to creat a boolean variable that =false, then when you get the object that opens the door, make the variable true? Then you could probobly make a script code that says that if you come in contact with the door and the boolean variable = true then open the door. Im not sure if it could work or not but you could do something like that.
Answer by Piflik · Apr 19, 2012 at 08:51 PM
Add a unique tag to the object (example 'Key'), add a trigger to both the key and the door and then add a script to the door:
function OnTriggerEnter(item) {
if(item.tag == "Key")
OpenDoor();
}
You have to either declare a OpenDoor() function, o replace OpenDoor() with whatever code you use, to actually open the door.