- Home /
how do i do a simple key door
this is probably a frequently asked question, but i have used and tried 5 different scripts, all with the same result.
it not working.
if anyone knows a script please link it to me, because all i'm looking for, is a door that opens as soon as i touch a trigger.
i tried to make my own at one point:
var door : Transform;
function OnTriggerStay (other : Collider) {
var contact : String = other.transform.name;
if (contact == "Player"){
Destroy(door);
Destroy(other.gameObject);
}
}
its clear this doesn't work either :I
$$anonymous$$ake sure that you have rigid bodies on both of your objects (the trigger and the player) otherwise the game won't be able to detect a collision. I'm assu$$anonymous$$g your trigger isn't destroyed either? This script is attached to your player, right?
pretty sure unity posted a tutorial about this on their website.
Answer by iwaldrop · Jun 15, 2013 at 06:00 AM
First off, you probably don't want to try and destroy the same things every frame.
But as MrThreepwood suggested, you need to make sure that you have colliders on things. Your player should have a collider and rigidbody, where as you door should have at least a collider whose Is Trigger box is checked. Then do something like this:
public GameObject door;
private int count;
void OnTriggerEnter(Collider other)
{
if (other.tag.Equals("Player"))
{
count++;
door.SetActive(false);
}
}
void OnTriggerExit(Collider other)
{
if (other.tag.Equals("Player") && --count < 1)
door.SetActive(true);
}
This will allow multiple players to enter the door collider area and will only close once all are out. Of course, for a single player game you can probably omit the counter.
Your answer
Follow this Question
Related Questions
How to make 2 doors with 2 diffrent keys 2 Answers
Pick up key and unlock a locked door 1 Answer
Scripting issue with opening a door with a key. 2 Answers
Locked door and key to open it script 1 Answer
Door Open With Object Pickup 1 Answer